Interface interaction between MFC ActiveX and JavaScript

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.