mercredi 24 décembre 2014

BHO extending javascript window object failing some times


I am trying to extend the internet explorer javascript window object with my bho class object, so that i can call the bho object methods from javascript. It is working in ie9 on windows 7 and most of the time in ie 11 on windows 8.1 pro, but IDispatchEx->InvokeEx failes with 0x80020003 Member not found error for the first page the browser navigates to, It succeeds on further page/tab navigations. I am including the code for NAVIGATECOMPLETE2 event handler code where iam extending the window object.


void STDMETHODCALLTYPE CHarmonyDTA::OnDocumentOpen(IDispatch *pDisp, VARIANT *pvarURL) { // Retrieve the top-level window from the site.



USES_CONVERSION;
HRESULT hr = S_OK;

// Query for the IWebBrowser2 interface.
CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;

// Is this event associated with the top-level browser?
if (spTempWebBrowser && m_spWebBrowser &&
m_spWebBrowser.IsEqualObject(spTempWebBrowser))
{
// Get the current document object from browser...
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if (SUCCEEDED(hr))
{
// ...and query for an HTML document.
CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
if (spHTMLDoc != NULL)
{ CComVariant result;
CComQIPtr<IHTMLWindow2> pWindow;
hr = spHTMLDoc->get_parentWindow(&pWindow);

IDispatchEx *pWndEx;
IDispatch * myidispptr;
DISPID dispid,putid;
DISPPARAMS dispparamsArgs;
VARIANT var;
DWORD dwProps;
putid = DISPID_PROPERTYPUT;
var.vt = VT_DISPATCH;
//this->QueryInterface(IID_IDispatch,(void **)&myidispptr);
var.pdispVal = this;
dispparamsArgs.cArgs = 1;
dispparamsArgs.rgvarg = &var;
dispparamsArgs.cNamedArgs = 0;
dispparamsArgs.rgdispidNamedArgs = NULL;

pWindow->QueryInterface(IID_IDispatchEx, (void **)&pWndEx);
hr = pWndEx->GetDispID(CComBSTR(L"HarmonyDTA"),fdexNameEnsure,&dispid);

hr = pWndEx->InvokeEx(dispid,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUTREF,&dispparamsArgs,NULL,NULL,NULL);

hr = pWindow->execScript(CComBSTR(L"function doload(param)\n {if(window.HarmonyDTA)\n {console.log(\"Sucsess\");\n confirm(\"HarmonyDTA exists\");}\n else{console.log(\"Failure\");confirm(\"HarmonyDTA doesnot exist\");}}\n window.addEventListener(\"load\", doload,false);"), CComBSTR(L"JavaScript"), &result);

}
}
}


}





Aucun commentaire:

Enregistrer un commentaire