HOWTO: Call a Script Function from a VC WebBrowser ApplicationID: Q185127
|
When hosting the WebBrowser control in a Visual C++ application, you may wish to execute a script function that exists on a Web page. This article demonstrates how to do this.
In order to call a script function that exists on a Web page, you have to use automation; in other words, IDispatch. Use the following steps to invoke a script function that exists on a Web page from your Visual C++ application:
#import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb
void CMyClass::ExecuteScriptFunction()
{
// m_WebBrowser is an instance of IWebBrowser2
MSHTML::IHTMLDocument2Ptr spDoc(m_WebBrowser.GetDocument());
if (spDoc)
{
IDispatchPtr spDisp(spDoc->GetScript());
if (spDisp)
{
// Evaluate is the name of the script function.
OLECHAR FAR* szMember = L"evaluate";
DISPID dispid;
HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispid);
if (SUCCEEDED(hr))
{
COleVariant vtResult;
static BYTE parms[] = VTS_BSTR;
COleDispatchDriver dispDriver(spDisp);
dispDriver.InvokeHelper(dispid, DISPATCH_METHOD, VT_VARIANT,
(void*)&vtResult, parms,
"5+Math.sin(9)");
}
}
}
}
<HTML>
<HEAD>
<TITLE>Evaluate</TITLE>
<SCRIPT>
function evaluate(x)
{
alert("hello")
return eval(x)
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
This source code is based on the Visual Basic sample that appears in the
March/April 1998 edition of MSDN News. Please refer to this edition for
information about how to execute a script function from a Visual Basic
application that is hosting the WebBrowser control.
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott
Roberts, Microsoft Corporation
Additional query words: kbDSupport kbdsi
Keywords : kbcode kbIE400 kbIE401 kbIE500 AXSDKWebBrowser
Version : WINDOWS:4.0,4.01,5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 1, 1999