comparison win/browser.c @ 2049:2bf8a22f515e

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.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 30 Nov 2019 13:34:01 +0000
parents cb195d76de8e
children 13995a6bf05d
comparison
equal deleted inserted replaced
2048:2d06f48e9d12 2049:2bf8a22f515e
1561 { 1561 {
1562 IWebBrowser2 *webBrowser2; 1562 IWebBrowser2 *webBrowser2;
1563 IHTMLWindow2 *htmlWindow2; 1563 IHTMLWindow2 *htmlWindow2;
1564 IHTMLDocument2 *htmlDocument2; 1564 IHTMLDocument2 *htmlDocument2;
1565 IOleObject *browserObject; 1565 IOleObject *browserObject;
1566 VARIANT result;
1567 int retval = DW_ERROR_UNKNOWN; 1566 int retval = DW_ERROR_UNKNOWN;
1568 1567
1569 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 1568 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
1570 // we initially attached the browser object to this window. 1569 // we initially attached the browser object to this window.
1571 browserObject = *((IOleObject **)dw_window_get_data(hwnd, "_dw_html")); 1570 browserObject = *((IOleObject **)dw_window_get_data(hwnd, "_dw_html"));
1578 1577
1579 if(!webBrowser2->lpVtbl->get_Document(webBrowser2, &pDp)) 1578 if(!webBrowser2->lpVtbl->get_Document(webBrowser2, &pDp))
1580 { 1579 {
1581 if (!pDp->lpVtbl->QueryInterface(pDp, &IID_IHTMLDocument2, (void**)&htmlDocument2)) 1580 if (!pDp->lpVtbl->QueryInterface(pDp, &IID_IHTMLDocument2, (void**)&htmlDocument2))
1582 { 1581 {
1583 if (!htmlDocument2->lpVtbl->get_parentWindow(htmlDocument2, &htmlWindow2)) 1582 BSTR myscript = SysAllocString(UTF8toWide(script));
1583 if (myscript)
1584 { 1584 {
1585 BSTR myscript = SysAllocString(UTF8toWide(script)); 1585 IDispatch* pScript = 0;
1586 if (myscript) 1586 HRESULT hr = E_FAIL;
1587 void* params[2];
1588 VARIANT result;
1589
1590 VariantInit(&result);
1591
1592 /* We first attempt to use the Invoke method */
1593 if(SUCCEEDED(htmlDocument2->lpVtbl->get_Script(htmlDocument2, &pScript)))
1587 { 1594 {
1588 HRESULT hr; 1595 DISPID idSave;
1589 void* params[2]; 1596 LPOLESTR rgszNames[1] = {L"eval"};
1590 1597
1591 VariantInit(&result); 1598 if(SUCCEEDED(pScript->lpVtbl->GetIDsOfNames(pScript, &IID_NULL, rgszNames, 1, LOCALE_SYSTEM_DEFAULT, &idSave)))
1599 {
1600 DISPPARAMS dispParams = {NULL, NULL, 0, 0};
1601 dispParams.cArgs = 1;
1602 dispParams.rgvarg = &myscript;
1603 hr = pScript->lpVtbl->Invoke(pScript, idSave, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispParams, &result, NULL, NULL);
1604 }
1605 pScript->lpVtbl->Release(pScript);
1606 }
1607 /* If Invoke fails, fall back to execScript */
1608 if(FAILED(hr) && SUCCEEDED(htmlDocument2->lpVtbl->get_parentWindow(htmlDocument2, &htmlWindow2)))
1609 {
1592 hr = htmlWindow2->lpVtbl->execScript(htmlWindow2, myscript, L"javascript", &result); 1610 hr = htmlWindow2->lpVtbl->execScript(htmlWindow2, myscript, L"javascript", &result);
1593 params[0] = (void*)(result.vt == VT_BSTR ? WideToUTF8(result.bstrVal) : NULL); 1611 // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it,
1594 params[1] = DW_INT_TO_POINTER((hr == S_OK ? DW_ERROR_NONE : DW_ERROR_UNKNOWN)); 1612 // so we can release our hold on it). Note that we'll still maintain our hold on the browser
1595 /* Pass the result back for event handling */ 1613 // object.
1596 _wndproc(hwnd, WM_USER+100, (WPARAM)params, (LPARAM)scriptdata); 1614 htmlWindow2->lpVtbl->Release(htmlWindow2);
1597 VariantClear(&result);
1598 SysFreeString(myscript);
1599 retval = DW_ERROR_NONE;
1600 } 1615 }
1601 // We no longer need the IWebBrowser2 object (ie, we don't plan to call any more functions in it, 1616 params[0] = (void*)(result.vt == VT_BSTR ? WideToUTF8(result.bstrVal) : NULL);
1602 // so we can release our hold on it). Note that we'll still maintain our hold on the browser 1617 params[1] = DW_INT_TO_POINTER((SUCCEEDED(hr) ? DW_ERROR_NONE : DW_ERROR_UNKNOWN));
1603 // object. 1618 /* Pass the result back for event handling */
1604 htmlWindow2->lpVtbl->Release(htmlWindow2); 1619 _wndproc(hwnd, WM_USER+100, (WPARAM)params, (LPARAM)scriptdata);
1620 VariantClear(&result);
1621 SysFreeString(myscript);
1622 retval = DW_ERROR_NONE;
1605 } 1623 }
1606 htmlDocument2->lpVtbl->Release(htmlDocument2); 1624 htmlDocument2->lpVtbl->Release(htmlDocument2);
1607 } 1625 }
1608 pDp->lpVtbl->Release(pDp); 1626 pDp->lpVtbl->Release(pDp);
1609 } 1627 }