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

后端 未结 1 849
梦毁少年i
梦毁少年i 2020-12-08 05:40

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.

I have fo

相关标签:
1条回答
  • 2020-12-08 06:26

    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 here for an example. If you want to use late-binding instead, you can use DISPIDs greater DISPID_VALUE and less than 0x80010000 (all values <= 0 and in the range 0x80010000 through 0x8001FFFF are reserved):

    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 map or constant values should be used.

    0 讨论(0)
提交回复
热议问题