How ActiveX calls a JavaScript function in a Web page that references that ActiveX

Source: Internet
Author: User
Tags object html page
active|activex|javascript| Function | Web page

Objective

Many netizens ask, in HTML page place a COM object, this object how to call the page script function problem, I recently also because the work needs to realize such function, after the internet search, got a lot of help, but did not find a complete implementation. This article describes several key points in the implementation process and provides a download of an example project.

1--
To add a method to an interface:
Method Name: SetClientSite
Parameters: ioleclientsite* pClientSite

2--
The above method is implemented (implemented in the corresponding class, and the framework is automatically generated):
STDMETHODIMP Cirdav::setclientsite (IOleClientSite * pclientsite)
{
TODO: Add implementation code here
HRESULT hr = S_OK;
IServiceProvider *isp, *isp2 = NULL;
if (!pclientsite)
{
Comrelease (M_spbrowser);
}
Else
{
Try
{
hr = Pclientsite->queryinterface (Iid_iserviceprovider, reinterpret_cast<void **> (&isp));
if (FAILED (HR))
Throw ("");
hr = Isp->queryservice (Sid_stoplevelbrowser, Iid_iserviceprovider, reinterpret_cast<void **> (&AMP;ISP2));
if (FAILED (HR))
Throw ("");
hr = Isp2->queryservice (Sid_swebbrowserapp, Iid_iwebbrowser2, reinterpret_cast<void **> (&m_spBrowser)) ;
if (FAILED (HR))
Throw ("");
Hr=this->m_spbrowser->get_document ((idispatch**) &m_spdoc);
if (FAILED (HR))
Throw ("");
}
catch (LPCTSTR Lpszerr)
{
if (CString (lpszerr)!= "")
MessageBox (Lpszerr);
HR=S_OK;
}
catch (...)
{
MessageBox ("Unknown error");
HR=S_OK;
}
Comrelease (ISP);
Comrelease (ISP2);
}
return HR;
}

With this step, we get the browser object pointer and the corresponding Document object pointer to the Web page where the control resides.
With these browser and document object pointers, we can set many of the window's properties, such as menus, Full-screen, toolbar display, document background color, and so on.
This method see msdn:http://support.microsoft.com/support/kb/articles/q257/7/17.asp

3--
Get the script, through the M_spdoc (Document Object) method Get_script can get the script object in the page:
Ccomptr<idispatch> Pscript;
Hr=this->m_spdoc->get_script (&pscript);

4--
Retrieves a function that uses the GetIDsOfNames of the script object above to obtain a pointer to the script function of the specified name in the page:
CComBSTR bstrmember ("Calert");
DISPID DISPID;
Hr=pscript->getidsofnames (IID_NULL,&BSTRMEMBER,1,LOCALE_SYSTEM_DEFAULT,&DISPID);

5--
Invoke method that invokes a script object executes a script function:
Dispparams Dispparams;
memset (&dispparams, 0, sizeof dispparams);
Dispparams.cargs = 2;
Dispparams.rgvarg = new Variant[dispparams.cargs];

for (int i = 0; i < 2; i++)
{
CComBSTR BSTR = "111"; Back reading
BStr. CopyTo (&dispparams.rgvarg[i].bstrval);
DISPPARAMS.RGVARG[I].VT = VT_BSTR;
}
Dispparams.cnamedargs = 0;
Excepinfo Excepinfo;
memset (&excepinfo, 0, sizeof excepinfo);
CComVariant Varesult;
UINT Nargerr = (UINT)-1; Initialize to invalid arg
hr = Pscript->invoke (dispid,iid_null,0,dispatch_method,&dispparams,&varesult,&excepinfo,& NARGERR);

Here, I randomly wrote a few parameters, can be modified according to their own needs.
This example triggers: Opens the appropriate HTM page, clicks the Send button, the button calls the script function Btn,btn calls the function KeyPress function in COM, obtains the Calert function in the KeyPress function and calls.

Note: one question:
To get the browser object is implemented in the SetClientSite function, which I don't know much about, I think should be one of the functions that the system runs automatically, but when I use this function, the events I defined in COM (such as the Click event) do not respond. Have a master said placed in another place, I think it should be, for example, to find a COM-ready function to deal with, but I do not know how to get ioleclientsite*pclientsite.

This project is used. NET2003, perhaps your system does not support, but it does not matter, important to establish their own COM project, the Irdav of the relevant documents to add to the project can be.

Sorry, because I do not have very stable space, can not provide downloads, if necessary, please give me QQ (68558710) or MSN (Triout@msn.com) or email (Triout@163.com) Contact. If you have room, please let us know in order to provide the connection after uploading.



Related Article

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.