网页如何调用Visual C++函数

1.在VC++工程中创建继承CHtml类的CMapView类,该类用于实现网页与VC++代码交互接口。

class CMapView : public CHtmlView
{
}

2.在CMapView类构造函数中添加:

EnableAutomation();

下面是微软官方文档对EnableAutomation()的定义:

Call this function to enable OLE automation for an object.

This function is typically called from the constructor of your object and should only be called if a dispatch map has been declared for the class. For more information on automation see the articles Automation Clients and Automation Servers.
只用在构造函数中调用了EnableAutomation(),CMapView()才能获取到网页的信息,否则会报错如下:


Debug Assertion Failed!   ............For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.

注:OLE automation现在已经更名为automation,它是一种机制,这种机制允许一个应用程序调用在另外一个应用程序实现的对象或者允许一个应用程序暴露对象供其他应用程序调用。

3.重写OnGetExternal()

微软官方文档对OnGetExternal()函数解释如下:

Override OnGetExternal to react to the GetExternal notification from the Microsoft Web Browser control. See IDocHostUIHandler::GetExternal in the Windows SDK for more information.

OnGetExternal()函数的主要作用就是从网页获取消息,如果重写此函数,网页消息无法映射到CMapView类,提示错误:当前页面的脚本发生错误:无法获取属性“函数名”的值:对象null或未定义。

并且值得OnGetExternal()调用遵循OLE机制,所以调用OnGetExternal()之前必须要在构造函数中调用EnableAutomation()。

3.1在CMapView.h文件中添加代码:

virtual HRESULT		OnGetExternal(LPDISPATCH *lppDispatch);
3.2在 CMapView.cpp文件中添加代码:

HRESULT CMapView::OnGetExternal(LPDISPATCH *lppDispatch)
{
	*lppDispatch = GetIDispatch(TRUE);
	return S_OK;
}
4.添加消息响应函数

4.1在CMapView.h文件中添加代码:

DECLARE_DISPATCH_MAP()
4.2在 CMapView.cpp文件中添加代码:

BEGIN_DISPATCH_MAP(CMapView, CCmdTarget)
	DISP_FUNCTION(CMapView, "CB_TDOALocation",CB_TDOALocation,VT_EMPTY, VTS_BSTR)
	DISP_FUNCTION(CMapView, "CB_TDOAStopLocation",CB_TDOAStopLocation,VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()

DISP_FUNCTION在一个dispatch map中定义一个OLE automation 函数,下面是他的语法结构:

DISP_FUNCTION(theClass, pszName, pfnMember, vtRetVal, vtsParams )

theClass

Name of the class.即CMapView类,继承CHtmlView类的类。

pszName

External name of the function.网页调用时使用的名字

pfnMember

Name of the member function.CMapView类中响应网页操作的函数名。

vtRetVal

A value specifying the function's return type.

vtsParams

A space-separated list of one or more constants specifying the function's parameter list.

vtRetVal和vtsParams包括很多种类型,更多信息详见MSDN中对DISP_FUNCTION的定义:ms-elp://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclib/html/8a1c666b-304b-4641-ab20-4f1a6d3c2d2e.htm

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值