![]() |
Platforms, Frameworks & Libraries »
COM / COM+ »
COM
Intermediate
JavaScript call from C++By Eugene KhodakovskyA class for easy implementation of JavaScript calls from C++ code |
VC6, VC7Win2K, WinXP, MFC, COM, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Sometimes when we are using the IE Browser Control inside of a C++ application
you need to get access to the HTML
elements.
We can do it by using standard COM objects like IWebBrowser2, IHTMLDocument2, etc.
By this method we can easy implement features like click button, click anchor, get input string, get HTML text, etc.
Unfortunately, Microsoft did not provide similar objects for JavaScript.
At any case to make a control for the JavaScript object inside of HTML page it is possible by using traditional COM approach.
This article describes the class CWebPage which allows to do it and technique to call Java Script Function from C++ code.
As the result of using presented class it will be easy to call any JavaScript function from C++ code.
For implement this feature we should to get pointer to the IHTMLDocument2 interface.
If we are using CHtmlView class from MFC we can get one by using member function
CHtmlView::GetHtmlDocument().
In case of using IWebBrowser or IWebBrowser2 components the function get_Document will bring us desired interface.
Here is example:
CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument(); m_webPage.SetDocument(spDisp);
The rest of all things will be done by CWebPage class. Here is example of JavaScript call without parameters.
m_webPage.CallJScript("Welcome");
The example of the JavaScript call with 2 parameters will looks like here.
m_webPage.CallJScript("Miltiply","2.34","3.32");
class CWebPage { public: CWebPage(); virtual ~CWebPage(); bool SetDocument(IDispatch* pDisp); LPDISPATCH GetHtmlDocument() const; const CString GetLastError() const; bool GetJScript(CComPtr<IDispatch>& spDisp); bool GetJScripts(CComPtr<IHTMLElementCollection>& spColl); CString ScanJScript(CString& strAText,CStringArray& args); bool CallJScript(const CString strFunc); bool CallJScript(const CString strFunc,const CString strArg1); bool CallJScript(const CString strFunc,const CString strArg1, const CString strArg2); bool CallJScript(const CString strFunc,const CString strArg1, const CString strArg2,const CString strArg3); bool CallJScript(const CString strFunc,const CStringArray& paramArray); protected: CComPtr<IHTMLDocument2> m_spDoc; };
The mentioned above technique splits on the following steps:
IHTMLDocument2 interface. IDispatch for JavaScript object in HTML document DISPID for given name of JavaScript function DISPPARAM structure IDispatch interfaceHere is example of getting IDispatch pointer to the Java Scripts objects
bool CWebPage::GetJScript(CComPtr<IDispatch>& spDisp) { HRESULT hr = m_spDoc->get_Script(&spDisp); ATLASSERT(SUCCEEDED(hr)); return SUCCEEDED(hr); }
And here is final function to call Java Script
CComVariant CWebPage::CallJScript(const CString strFunc, const CStringArray& paramArray) { //Getting IDispatch for Java Script objects CComPtr<IDispatch> spScript; if(!GetJScript(spScript)) { ShowError("Cannot GetScript"); return false; } //Find dispid for given function in the object CComBSTR bstrMember(strFunc); DISPID dispid = NULL; HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1, LOCALE_SYSTEM_DEFAULT,&dispid); if(FAILED(hr)) { ShowError(GetSystemErrorMessage(hr)); return false; } const int arraySize = paramArray.GetSize(); //Putting parameters DISPPARAMS dispparams; memset(&dispparams, 0, sizeof dispparams); dispparams.cArgs = arraySize; dispparams.rgvarg = new VARIANT[dispparams.cArgs]; dispparams.cNamedArgs = 0; for( int i = 0; i < arraySize; i++) { CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i); // back reading bstr.CopyTo(&dispparams.rgvarg[i].bstrVal); dispparams.rgvarg[i].vt = VT_BSTR; } EXCEPINFO excepInfo; memset(&excepInfo, 0, sizeof excepInfo); CComVariant vaResult; UINT nArgErr = (UINT)-1; // initialize to invalid arg //Call JavaScript function hr = spScript->Invoke(dispid,IID_NULL,0, DISPATCH_METHOD,&dispparams, &vaResult,&excepInfo,&nArgErr); delete [] dispparams.rgvarg; if(FAILED(hr)) { ShowError(GetSystemErrorMessage(hr)); return false; } return vaResult; }
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 May 2002 Editor: Chris Maunder |
Copyright 2002 by Eugene Khodakovsky Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |