After searching around for a couple of hours, I managed to make it run as required. The following is a snippet for how to make it work (runs within
DOCUMENTCOMPLETE
):
CComPtr<ihtmlwindow2> win;
doc2->get_parentWindow( &win);
if (win == NULL) return E_FAIL;
CComDispatchDriver dispWindow;
win->QueryInterface(&dispWindow);
if (dispWindow == NULL) return E_FAIL;
CComBSTR fullScript = L"function testFunction(){";
fullScript += L"alert('JS function called!')";
fullScript += L"return true;";
fullScript += L"}testFunction();";
CComVariant script(fullScript.m_str);
CComVariant result;
dispWindow.Invoke1(L"eval", &script, &result);
result.ChangeType(VT_BSTR);
LOG(L"%s", result.bstrVal);
</ihtmlwindow2>