windows 从 Web 浏览器控件中运行的 JavaScript 脚本调用 C++ 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3747414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:16:50  来源:igfitidea点击:

Calling C++ function from JavaScript script running in a web browser control

javascriptc++windowscomiwebbrowser2

提问by Tobbe

I have embedded a web browser control in my c++ application. I want javascript running in the web browser control to be able to call a c++ function/method.

我在我的 C++ 应用程序中嵌入了一个 Web 浏览器控件。我希望在 Web 浏览器控件中运行的 javascript 能够调用 C++ 函数/方法。

I have found mentions of three ways to do this:

我发现提到了三种方法:

  1. Implement an ActiveX component that acts as a middle man. (Implementation details here: http://blogs.msdn.com/b/nicd/archive/2007/04/18/calling-into-your-bho-from-a-client-script.aspx)
  2. Use window.external. (Also discussed in the link above, but no implementation provided)
  3. Add a custom object to the window object
  1. 实现充当中间人的 ActiveX 组件。(这里的实现细节:http: //blogs.msdn.com/b/nicd/archive/2007/04/18/calling-into-your-bho-from-a-client-script.aspx
  2. 使用 window.external。(也在上面的链接中讨论过,但没有提供实现)
  3. 向窗口对象添加自定义对象

I want to go with the third option, but I haven't found any working examples on how to do that. Can someone please show me how to do it, or link to a working example on the net somewhere.

我想选择第三个选项,但我还没有找到任何关于如何做到这一点的有效示例。有人可以告诉我如何做,或链接到网络上某处的工作示例。

The closest to an example that I have found is the first reply by Igor Tandetnik in a thread in the webbrowser_ctl news group. But I'm afraid I need more help than that.

我发现的最接近示例的是 Igor Tandetnik 在webbrowser_ctl 新闻组中的一个线程中的第一个回复。但恐怕我需要更多的帮助。

I'm embedding an IWebBrowser2 control and I am not using MFC, ATL or WTL.

我嵌入了一个 IWebBrowser2 控件并且我没有使用 MFC、ATL 或 WTL。

EDIT:

编辑:

Going by the pseudo-code given by Igor in the thread I linked earlier, and code found in the codeproject article "Creating JavaScript arrays and other objects from C++" I've produced some code.

根据 Igor 在我之前链接的线程中给出的伪代码,以及在 codeproject 文章“从 C++ 创建 JavaScript 数组和其他对象”中找到的代码,我生成了一些代码。

void WebForm::AddCustomObject(IDispatch *custObj, std::string name)
{
    IHTMLDocument2 *doc = GetDoc();
    IHTMLWindow2 *win = NULL;
    doc->get_parentWindow(&win);

    if (win == NULL) {
        return;
    }

    IDispatchEx *winEx;
    win->QueryInterface(&winEx);

    if (winEx == NULL) {
        return;
    }

    int lenW = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name.c_str(), -1, NULL, 0);
    BSTR objName = SysAllocStringLen(0, lenW);
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name.c_str(), -1, objName, lenW);

    DISPID dispid; 
    HRESULT hr = winEx->GetDispID(objName, fdexNameEnsure, &dispid);

    SysFreeString(objName);

    if (FAILED(hr)) {
        return;
    }

    DISPID namedArgs[] = {DISPID_PROPERTYPUT};
    DISPPARAMS params;
    params.rgvarg = new VARIANT[1];
    params.rgvarg[0].pdispVal = custObj;
    params.rgvarg[0].vt = VT_DISPATCH;
    params.rgdispidNamedArgs = namedArgs;
    params.cArgs = 1;
    params.cNamedArgs = 1;

    hr = winEx->InvokeEx(dispid, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, &params, NULL, NULL, NULL); 

    if (FAILED(hr)) {
        return;
    }
}

The code above runs all the way through, so everything looks alright that far.

上面的代码一直运行,所以到目前为止一切看起来都很好。

I call AddCustomObject when I receive the DISPID_NAVIGATECOMPLETE2 DWebBrowserEvents2 event passing this as *custObj:

当我收到 DISPID_NAVIGATECOMPLETE2 DWebBrowserEvents2 事件时,我调用 AddCustomObject 将其传递为*custObj

class JSObject : public IDispatch {
private:
    long ref;

public:
    // IUnknown
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppv);
    virtual ULONG STDMETHODCALLTYPE AddRef();
    virtual ULONG STDMETHODCALLTYPE Release();

    // IDispatch
    virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *pctinfo);
    virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid,
        ITypeInfo **ppTInfo);
    virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
    virtual HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid,
        LCID lcid, WORD wFlags, DISPPARAMS *Params, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr);
};

Noteworthy implementations might be

值得注意的实现可能是

HRESULT STDMETHODCALLTYPE JSObject::QueryInterface(REFIID riid, void **ppv)
{
    *ppv = NULL;

    if (riid == IID_IUnknown || riid == IID_IDispatch) {
        *ppv = static_cast<IDispatch*>(this);
    }

    if (*ppv != NULL) {
        AddRef();
        return S_OK;
    }

    return E_NOINTERFACE;
}

and

HRESULT STDMETHODCALLTYPE JSObject::Invoke(DISPID dispIdMember, REFIID riid,
    LCID lcid, WORD wFlags, DISPPARAMS *Params, VARIANT *pVarResult,
    EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    MessageBox(NULL, "Invoke", "JSObject", MB_OK);
    return DISP_E_MEMBERNOTFOUND;
}

Unfortunately I never get the "Invoke" message box when I try to use the "JSObject" object from the javascript code.

不幸的是,当我尝试使用 javascript 代码中的“JSObject”对象时,我从未收到“调用”消息框。

JSObject.randomFunctionName();  // This should give me the c++ "Invoke" message
                                // box, but it doesn't

EDIT 2:

编辑2:

I implemented GetIDsOfNameslike so:

GetIDsOfNames是这样实现的:

HRESULT STDMETHODCALLTYPE JSObject::GetIDsOfNames(REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    HRESULT hr = S_OK;

    for (UINT i = 0; i < cNames; i++) {
        std::map<std::wstring, DISPID>::iterator iter = idMap.find(rgszNames[i]);
        if (iter != idMap.end()) {
            rgDispId[i] = iter->second;
        } else {
            rgDispId[i] = DISPID_UNKNOWN;
            hr = DISP_E_UNKNOWNNAME;
        }
    }

    return hr;
}

and this is my constructor

这是我的构造函数

JSObject::JSObject() : ref(0)
{
    idMap.insert(std::make_pair(L"execute", DISPID_USER_EXECUTE));
    idMap.insert(std::make_pair(L"writefile", DISPID_USER_WRITEFILE));
    idMap.insert(std::make_pair(L"readfile", DISPID_USER_READFILE));
}

with the DISPID_USER_* constants defined as private class members

将 DISPID_USER_* 常量定义为私有类成员

class JSObject : public IDispatch {
private:
    static const DISPID DISPID_USER_EXECUTE = DISPID_VALUE + 1;
    static const DISPID DISPID_USER_WRITEFILE = DISPID_VALUE + 2;
    static const DISPID DISPID_USER_READFILE = DISPID_VALUE + 3;

    // ...
};

EDIT 3, 4 and 5:

编辑 3、4 和 5:

Moved to a separate question

移到一个单独的问题

EDIT 6:

编辑 6:

Made a separate questionout of the "returning a string" edits. That way I can accept Georg'sreply as that answers the original question.

一个单独的问题了“返回一个字符串”的编辑。这样我就可以接受Georg 的回答,因为它回答了最初的问题。

EDIT 7:

编辑 7:

I have gotten a few requests for a fully working, self contained, example implementation. Here it is: https://github.com/Tobbe/CppIEEmbed. Please fork and improve if you can :)

我收到了一些关于完全工作、自包含的示例实现的请求。它是:https://github.com/Tobbe/CppIEEmbed。如果可以,请分叉并改进:)

采纳答案by Georg Fritzsche

You need to implement GetIDsOfNames()to do something sensible as that function will be called by client code before Invoke().
If you have your interfaces in a type library see herefor an example. If you want to use late-binding instead, you can use DISPIDsgreater DISPID_VALUEand less than 0x80010000(all values <= 0and in the range 0x80010000through 0x8001FFFFare reserved):

您需要执行GetIDsOfNames()一些明智的操作,因为该函数将在Invoke().
如果您在类型库中有接口,请参见此处的示例。如果你想使用,而不是后期绑定,你可以用DISPID小号大于DISPID_VALUE和小于0x80010000(所有值<= 0和范围0x80010000,通过0x8001FFFF保留):

HRESULT GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, 
                      LCID lcid, DISPID *rgDispId)
{
    HR hr = S_OK;
    for (UINT i=0; i<cNames; ++i) {
        if (validName(rgszNames)) {
            rgDispId[i] = dispIdForName(rgszNames);
        } else {
            rgDispId[i] = DISPID_UNKNOWN;
            hr = DISP_E_UNKNOWNNAME;
        }
    }
    return hr;
}

HRESULT Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, 
               DISPPARAMS *Params, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, 
               UINT *puArgErr)
{
    if (wFlags & DISPATCH_METHOD) {
       // handle according to DISPID ...
    }

    // ...

Note that the DISPIDs are not supposed to change suddenly, so e.g. a static mapor constant values should be used.

请注意,DISPIDs 不应突然更改,因此map应使用例如静态或常量值。