tuma537629

Hi,

I am writing a BHO for taking some addtional control over current HTML page. In DocumentComplete event, I am trying to execute a javascript using HTMLWindow2 execScript. But I am getting E_INVALIDARG error from this call.

CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spDoc = spDispatch;

if (spDoc != NULL)
{
IHTMLWindow2 *spWindow;
if ( SUCCEEDED( spDoc->get_parentWindow( &spWindow))) {
VARIANT pvarRet;
HRESULT hr = spWindow->execScript(L" alert(\"tuma\"); ", L"javascript", &pvarRet);
if (!SUCCEEDED(hr)) {
::MessageBox(NULL, "execution failed..", "javascript", 0);
}
spWindow->Release();
}
}

I am new to this IE extension development. Please help.

-tuma




Re: Internet Explorer Extension Development IHTMLWindow2::execScript returns E_INVALIDARG

Reza Nourai - MSFT

You should be passing in BSTRs and not WCHAR* like you are. Also, the documentation says that pvarRet should be set to VT_EMPTY. Try:

CComBSTR bstrScript = L"alert(\"tuma\");";

CComBSTR bstrLanguage = L"javascript";

VARIANT vEmpty = {0};

spWindow->execScript(bstrScript,bstrLanguage,&vEmpty);

See if that works.

-Reza





Re: Internet Explorer Extension Development IHTMLWindow2::execScript returns E_INVALIDARG

zmike

try get_Script instead of get_parentWindow

also once i notice that better to use ' instead " in alert...

CComPtr<IDispatch> spDisp;
spDoc->get_Script( &spDisp );
CComQIPtr<IHTMLWindow2> spWin = spDisp;
VARIANT pvarRet;
HRESULT tt = spWin->execScript(_bstr_t("alert(1)"),_bstr_t("JavaScript"),&pvarRet);





Re: Internet Explorer Extension Development IHTMLWindow2::execScript returns E_INVALIDARG

tuma

Hi All,

Thanks for your reply. I tried with Reza's solution. It works!

-tuma