3

我找到了将数组从 Javascript 传递到 C++解决方案,但我还有另一个任务:将对象从 Javascript 传递到 C++(如果我将 IWebBrowser2 与 IDispatch 一起使用)

我的意思是我需要通过带有 JavaScript 对象参数的 window.external.method 调用 C++ 方法

var obj = {name: "Petr", group: "Friend"};
window.external.myMethod(obj);

如何访问对象成员“名称”、“组”等?

4

3 回答 3

2

您可以通过IDispatch接口及其方法GetIDsOfNamesInvoke.

根据您对 的定义myMethod,您应该在 C++ 代码中obj以 aVARIANT或 an的形式接收。IDispatch *如果是VARIANTvt应该是VT_DISPACTH,在这种情况下,您可以安全地取消引用pdispval

一旦你有了一个IDispatch指针,你就可以使用它GetIDsOfNames来获取DISPID你感兴趣的属性,如下所示:

_bstr_t sPropertyName = L"myProperty";
DISPID dispid = 0;
HRESULT hr = pDispatch->GetIDsOfNames(IID_NULL, &sPropertyName, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if(SUCCEEDED(hr))
{
    ...

一旦你成功收到你的DISPID,你必须Invoke根据你想要获取一个值、设置一个值还是调用一个方法来进行不同的调用。

例如,要获取一个值:

    VARIANT vValue;
    hr = pDispatch->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, 0, &vValue, 0, 0);
    if(SUCCEEDED(hr))
    {
        ...

Invoke有关调用它时的不同排列的更多信息,请参阅文档。

于 2011-05-18T11:02:11.613 回答
0

If you just need to be able to read the object fields, the most flexible way is to use JSON.

On your web page side use:

var obj = {name: "Petr", group: "Friend"}; window.external.myMethod(JSON.stringify(obj));

On your C++ side define myMethod() to accept a single string argument. Then use a C++ JSON parser to parse this argument into a readable object.

于 2011-05-17T15:52:44.260 回答
0
static HRESULT \
IDispatch_VarGet (IDispatch *pRDisp, LPOLESTR Name, VARIANT *pVarRes)
{
    DISPPARAMS DispParams;
    DISPID dispid;
    HRESULT hr;

    if ((hr = IDispatch_GetIDsOfNames (pRDisp, &IID_NULL,
                &Name, 1, LOCALE_SYSTEM_DEFAULT, &dispid)))
        return hr;

    ZeroMemory (&DispParams, sizeof (DispParams));

    hr = IDispatch_Invoke (pRDisp, dispid, &IID_NULL,
            LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
            &DispParams, pVarRes, NULL, NULL);

    return hr;
}


static HRESULT \
IDispatch_VarPut (IDispatch *pRDisp, LPOLESTR Name, VARIANT *pVarArg)
{
    DISPPARAMS DispParams;
    DISPID dispid, ndispid;
    HRESULT hr;

    if ((hr = IDispatch_GetIDsOfNames (pRDisp, &IID_NULL,
                &Name, 1, LOCALE_SYSTEM_DEFAULT, &dispid)))
        return hr;

    ndispid = DISPID_PROPERTYPUT;

    DispParams.rgvarg = pVarArg;
    DispParams.rgdispidNamedArgs = &ndispid;
    DispParams.cArgs  = 1;
    DispParams.cNamedArgs = 1;

    hr = IDispatch_Invoke (pRDisp, dispid, &IID_NULL,
            LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT,
            &DispParams, NULL, NULL, NULL);

    return hr;
}


static HRESULT \
IDispatch_StrPut (IDispatch *pRDisp, LPOLESTR Name, LPOLESTR StrVal)
{
    VARIANT varg;
    HRESULT hr;

    VariantInit (&varg);

    V_VT   (&varg) = VT_BSTR;
    V_BSTR (&varg) = SysAllocString (StrVal);

    hr = IDispatch_VarPut (pRDisp, Name, &varg);

    VariantClear (&varg);

    return hr;
}


static HRESULT \
IDispatch_IntPut (IDispatch *pRDisp, LPOLESTR Name, int IntVal)
{
    VARIANT varg;
    HRESULT hr;

    VariantInit (&varg);

    V_VT (&varg) = VT_I4;
    V_I4 (&varg) = IntVal;

    hr = IDispatch_VarPut (pRDisp, Name, &varg);

    VariantClear (&varg);

    return hr;
}

...

if ((hr = IDispatch_StrPut (pRDisp, L"code", buff)))
    return hr;

if ((hr = IDispatch_IntPut (pRDisp, L"state", state)))
    return hr;

...

{
    // ...
    VariantInit (&varg);

    if ((hr = IDispatch_VarGet (pRDisp, L"code", &varg)))
    {
        VariantClear (&varg);
        return hr;
    }

    if (V_VT (&varg) == VT_BSTR)

    // ...

    VariantClear (&varg);
    return hr;
}

和 java 代码示例...

var frec = {code:'', state:0};

frec.code = row.cells[0].innerHTML;
external.FeatureStateChg (frec);
_featState (row.cells[2], frec.state);

...

var frec = {code:'', name:'', state:0};

if (!external.FeaturesEnum (frec, i))
    break;
于 2013-12-18T20:57:42.823 回答