Interface interaction between MFC ActiveX and JavaScript
In ActiveX applications, JavaScript interaction with Web pages is essential. Here is a brief introduction.
There are two types of ActiveX created by VC: MFC ActiveX and ATL ActiveX, which are selected when Vc creates a project.ArticleThis section describes the MFC ActiveX, which can be bypassed by ATL ActiveX.
Test environment: VC ++ 6.0 SP6
(1) transmission parameters and return values of MFC ActiveX and JavaScript
ActiveX methods can be divided into two types for JavaScript calls:
1. If the input parameter is an integer variable, the return value is an integer variable.
LongCactivexctrl: func (LongI)
{
Return 0;
}
2. the parameter is a string variable and the return value is a string variable.
BSTR cactivexctrl: func (lpctstr Str)
{
Cstring strresult;
Strresult ="Teaststring2 RET";
ReturnStrresult. allocsysstring ();
}
3 .... For other call methods, refer to the preceding two methods.
4. By default, all variables in javascript can be of the variant type, so that we can use variant to pass Parameters of various types. In ActiveX, type conversion is performed on variant variables first, then call. When a result is returned, the result is packaged into a variable of the variant type and then returned. (Transformation between variant and various types of variables is not the focus of this article. I will not explain it here)
Variant cactivexctrl: func (ConstVariant far & para1)
{
Variant varesult;
Variantinit (& varesult );
ReturnVaresult;
}
(2) MFC ActiveX asynchronously calls JavaScript event Methods
1. register the callback function in MFC ActiveX:
// Callback Function Test
# Include <atlbase. h>
Extern Ccommodule _ module;
# Include <atlcom. h>
// Callback Function-global variable
Ccomdispatchdriver m_func1;
// Register callback Functions
Void Cactivexctrl: regfun (lpdispatch bstrfun)
{
M_func1 = bstrfun;
}
// Trigger callback function
Void Cactivexctrl: func_calljs ()
{
Variant vararg [ 1 ];
Vararg [ 0 ]. Vt = vt_uint;
Vararg [ 0 ]. Uintval = 10 ;
M_func1.invoken (dispid) dispid_value, vararg, 1 );
}
2. register the asynchronous process in javascript:
//Asynchronous callback function Process
Function callfun (I)
{
Alert ("Callback Function in JS, ret ="+ I );
}
//Register callback Functions
OBJ. regfun (callfun );
From: http://www.cnblogs.com/sding/archive/2012/04/03/2431142.html