# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1575120841 0 # Node ID 2bf8a22f515e38911d404e83feb8cdf29c4542f4 # Parent 2d06f48e9d127e59dd7521ed81fb8c7d68b2a271 Win: Attempt to move to Invoke(eval) from the deprecated execScript() method. execScript() is deprecated and does not seem to be able to return the result. This is the first attempt, it gets to Invoke() but Invoke() fails, it then falls back to the older execScript() so the failure is not visible. diff -r 2d06f48e9d12 -r 2bf8a22f515e win/browser.c --- a/win/browser.c Sat Nov 30 10:43:55 2019 +0000 +++ b/win/browser.c Sat Nov 30 13:34:01 2019 +0000 @@ -1563,7 +1563,6 @@ IHTMLWindow2 *htmlWindow2; IHTMLDocument2 *htmlDocument2; IOleObject *browserObject; - VARIANT result; int retval = DW_ERROR_UNKNOWN; // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when @@ -1580,28 +1579,47 @@ { if (!pDp->lpVtbl->QueryInterface(pDp, &IID_IHTMLDocument2, (void**)&htmlDocument2)) { - if (!htmlDocument2->lpVtbl->get_parentWindow(htmlDocument2, &htmlWindow2)) + BSTR myscript = SysAllocString(UTF8toWide(script)); + if (myscript) { - BSTR myscript = SysAllocString(UTF8toWide(script)); - if (myscript) + IDispatch* pScript = 0; + HRESULT hr = E_FAIL; + void* params[2]; + VARIANT result; + + VariantInit(&result); + + /* We first attempt to use the Invoke method */ + if(SUCCEEDED(htmlDocument2->lpVtbl->get_Script(htmlDocument2, &pScript))) { - HRESULT hr; - void* params[2]; + DISPID idSave; + LPOLESTR rgszNames[1] = {L"eval"}; - VariantInit(&result); + if(SUCCEEDED(pScript->lpVtbl->GetIDsOfNames(pScript, &IID_NULL, rgszNames, 1, LOCALE_SYSTEM_DEFAULT, &idSave))) + { + DISPPARAMS dispParams = {NULL, NULL, 0, 0}; + dispParams.cArgs = 1; + dispParams.rgvarg = &myscript; + hr = pScript->lpVtbl->Invoke(pScript, idSave, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispParams, &result, NULL, NULL); + } + pScript->lpVtbl->Release(pScript); + } + /* If Invoke fails, fall back to execScript */ + if(FAILED(hr) && SUCCEEDED(htmlDocument2->lpVtbl->get_parentWindow(htmlDocument2, &htmlWindow2))) + { hr = htmlWindow2->lpVtbl->execScript(htmlWindow2, myscript, L"javascript", &result); - params[0] = (void*)(result.vt == VT_BSTR ? WideToUTF8(result.bstrVal) : NULL); - params[1] = DW_INT_TO_POINTER((hr == S_OK ? DW_ERROR_NONE : DW_ERROR_UNKNOWN)); - /* Pass the result back for event handling */ - _wndproc(hwnd, WM_USER+100, (WPARAM)params, (LPARAM)scriptdata); - VariantClear(&result); - SysFreeString(myscript); - retval = DW_ERROR_NONE; + // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it, + // so we can release our hold on it). Note that we'll still maintain our hold on the browser + // object. + htmlWindow2->lpVtbl->Release(htmlWindow2); } - // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it, - // so we can release our hold on it). Note that we'll still maintain our hold on the browser - // object. - htmlWindow2->lpVtbl->Release(htmlWindow2); + params[0] = (void*)(result.vt == VT_BSTR ? WideToUTF8(result.bstrVal) : NULL); + params[1] = DW_INT_TO_POINTER((SUCCEEDED(hr) ? DW_ERROR_NONE : DW_ERROR_UNKNOWN)); + /* Pass the result back for event handling */ + _wndproc(hwnd, WM_USER+100, (WPARAM)params, (LPARAM)scriptdata); + VariantClear(&result); + SysFreeString(myscript); + retval = DW_ERROR_NONE; } htmlDocument2->lpVtbl->Release(htmlDocument2); }