如何在C++中调用IE中的eval()?

Jim*_*ans 13 c++ com internet-explorer

随着IE11的出现,IHTMLWindow2::execScript()不推荐使用.推荐的方法是使用eval()代替.我通过其C++ COM接口自动化IE,我一直无法找到如何实现这一点.有人能指出我在搜索中明显错过的例子吗?如果无法执行代码eval,那么将JavaScript代码注入到现在execScript不再可用的Internet Explorer实例中的适当方法是什么?

编辑:任何适用于我正在进行的项目的解决方案必须在进程外工作.我没有使用浏览器助手对象(BHO)或任何类型的IE插件.因此,任何涉及无法正确编组交叉过程的接口的解决方案都不适用于我.

nos*_*tio 13

我现在已经验证了该eval方法与IE9,IE10和IE11一致(错误检查跳过了错误):

CComVariant result;
CComDispatchDriver disp = m_htmlWindow; // of IHTMLWindow2
disp.Invoke1(L"eval", &CComVariant(L"confirm('See this?')"), &result);
result.ChangeType(VT_BSTR);
MessageBoxW(V_BSTR(&result));
Run Code Online (Sandbox Code Playgroud)

感觉甚至比execScript,因为它实际上返回了result.它也适用于C#with WinForms' WebBrowser:

var result = webBrowser1.Document.InvokeScript("eval", new object[] { "confirm('see this?')" });
MessageBox.Show(result.ToString());
Run Code Online (Sandbox Code Playgroud)

也就是说,execScript仍适用于IE11 Preview:

CComVariant result;
m_htmlWindow->execScript(CComBSTR(L"confirm('See this too?')"), CComBSTR(L"JavaScript"), &result);
result.ChangeType(VT_BSTR);
MessageBoxW(V_BSTR(&result));
Run Code Online (Sandbox Code Playgroud)

它仍然result像往常一样丢弃它.

有点偏离主题,但你不必eval为此而坚持.此方法允许执行window加载页面的JavaScript 对象的名称空间内可用的任何命名方法(通过IDispatch接口).您可以调用自己的函数并将实时COM对象传递给它,而不是字符串参数,例如:

// JavaScript
function AlertUser(user)
{
  alert(user.name);
  return user.age;
}

// C++
CComDispatchDriver disp = m_htmlWindow; // of IHTMLWindow2
disp.Invoke1(L"AlertUser", &CComVariant(userObject), &result);
Run Code Online (Sandbox Code Playgroud)

我希望eval在可能的情况下直接打电话.

[EDITED]

需要进行一些调整才能使此方法适用于进程外调用.正如@JimEvans在评论中指出的那样,Invoke返回错误0x80020006("未知名称").但是,测试HTA应用程序运行得很好,是什么让我想到尝试IDispatchEx :: GetDispId进行名称解析.确实有效(跳过错误检查):

CComDispatchDriver dispWindow;
htmlWindow->QueryInterface(&dispWindow);

CComPtr<IDispatchEx> dispexWindow;
htmlWindow->QueryInterface(&dispexWindow);

DISPID dispidEval = -1;
dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispidEval);
dispWindow.Invoke1(dispidEval, &CComVariant("function DoAlert(text) { alert(text); }")); // inject

DISPID dispidDoAlert = -1;
dispexWindow->GetDispID(CComBSTR("DoAlert"), fdexNameCaseSensitive, &dispidDoAlert) );
dispWindow.Invoke1(dispidDoAlert, &CComVariant("Hello, World!")); // call
Run Code Online (Sandbox Code Playgroud)

完整的C++测试应用程序在这里:http://pastebin.com/ccZr0cG2

[UPDATE]

此更新__execScriptwindow子进程外创建方法iframe.要注入的代码已经过优化,可以返回目标window对象供以后使用(不需要进行一系列的out-of-proc调用来获取iframe对象,它是在主窗口的上下文中完成的):

CComBSTR __execScriptCode(L"(window.__execScript = function(exp) { return eval(exp); }, window.self)");
Run Code Online (Sandbox Code Playgroud)

下面是C++控制台应用程序(pastebin)的代码,跳过一些错误检查以查找缺陷.在.HTA中还有一个相应的原型,它更具可读性.

//
// http://stackoverflow.com/questions/18342200/how-do-i-call-eval-in-ie-from-c/18349546//
//

#include <tchar.h>
#include <ExDisp.h>
#include <mshtml.h>
#include <dispex.h>
#include <atlbase.h>
#include <atlcomcli.h>

#define _S(a) \
    { HRESULT hr = (a); if (FAILED(hr)) return hr; } 

#define disp_cast(disp) \
    ((CComDispatchDriver&)(void(static_cast<IDispatch*>(disp)), reinterpret_cast<CComDispatchDriver&>(disp)))

struct ComInit {
    ComInit() { ::CoInitialize(NULL); }
    ~ComInit() { CoUninitialize(); }
};

int _tmain(int argc, _TCHAR* argv[])
{
    ComInit comInit;

    CComPtr<IWebBrowser2> ie;
    _S( ie.CoCreateInstance(L"InternetExplorer.Application", NULL, CLSCTX_LOCAL_SERVER) );
    _S( ie->put_Visible(VARIANT_TRUE) );
    CComVariant ve;
    _S( ie->Navigate2(&CComVariant(L"http://jsfiddle.net/"), &ve, &ve, &ve, &ve) );

    // wait for page to finish loading
    for (;;)
    {
        Sleep(250);
        READYSTATE rs = READYSTATE_UNINITIALIZED;
        ie->get_ReadyState(&rs);
        if ( rs == READYSTATE_COMPLETE )
            break;
    }

    // inject __execScript into the main window

    CComPtr<IDispatch> dispDoc;
    _S( ie->get_Document(&dispDoc) );
    CComPtr<IHTMLDocument2> htmlDoc;
    _S( dispDoc->QueryInterface(&htmlDoc) );
    CComPtr<IHTMLWindow2> htmlWindow;
    _S( htmlDoc->get_parentWindow(&htmlWindow) );
    CComPtr<IDispatchEx> dispexWindow;
    _S( htmlWindow->QueryInterface(&dispexWindow) );

    CComBSTR __execScript("__execScript");
    CComBSTR __execScriptCode(L"(window.__execScript = function(exp) { return eval(exp); }, window.self)");

    DISPID dispid = -1;
    _S( dispexWindow->GetDispID(CComBSTR("eval"), fdexNameCaseSensitive, &dispid) );
    _S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(__execScriptCode)) ); 

    // inject __execScript into the child frame

    WCHAR szCode[1024];
    wsprintfW(szCode, L"document.all.tags(\"iframe\")[0].contentWindow.eval(\"%ls\")", __execScriptCode.m_str);

    dispid = -1;
    _S( dispexWindow->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
    CComVariant vIframe;
    _S( disp_cast(dispexWindow).Invoke1(dispid, &CComVariant(szCode), &vIframe) ); // inject __execScript and return the iframe's window object
    _S( vIframe.ChangeType(VT_DISPATCH) );

    CComPtr<IDispatchEx> dispexIframe;
    _S( V_DISPATCH(&vIframe)->QueryInterface(&dispexIframe) );

    dispid = -1;
    _S( dispexIframe->GetDispID(__execScript, fdexNameCaseSensitive, &dispid) );
    _S( disp_cast(dispexIframe).Invoke1(dispid, &CComVariant("alert(document.URL)")) ); // call the code inside child iframe

    return 0;
}
Run Code Online (Sandbox Code Playgroud)