首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

怎么在CDHtmldialog中调用JS函数(JS函数有参数)

2014-07-14 
如何在CDHtmldialog中调用JS函数(JS函数有参数)C/C++ codeIHTMLDocument2*pDocument;HRESULThr=GetDHtmlDo

如何在CDHtmldialog中调用JS函数(JS函数有参数)

C/C++ code
      IHTMLDocument2*   pDocument;         HRESULT   hr   =   GetDHtmlDocument(&pDocument);         IHTMLWindow2*   pWindow;         hr   =   pDocument->get_parentWindow(&pWindow);         VARIANT   ret;         ret.vt     =   VT_EMPTY;       CString strfunc1("myfun()");      hr   =   pWindow->execScript(L"myfun()",   L"javascript",&ret);

上面的代码能调用成功,但是如果myfun()中有参数就不行了,如下:
C/C++ code
      IHTMLDocument2*   pDocument;         HRESULT   hr   =   GetDHtmlDocument(&pDocument);         IHTMLWindow2*   pWindow;         hr   =   pDocument->get_parentWindow(&pWindow);         VARIANT   ret;         ret.vt     =   VT_EMPTY;       CString h("fad");      h.AllocSysString();      CString strfunc1("myfun(h)");      hr   =   pWindow->execScript(strfunc1.AllocSysString(),   L"javascript",&ret);

提示h未定义
这样也不行:
C/C++ code
      IHTMLDocument2*   pDocument;         HRESULT   hr   =   GetDHtmlDocument(&pDocument);         IHTMLWindow2*   pWindow;         hr   =   pDocument->get_parentWindow(&pWindow);         VARIANT   ret;         ret.vt     =   VT_EMPTY;       CString strfunc1("myfun(/'h/')");      hr   =   pWindow->execScript(strfunc1.AllocSysString(),   L"javascript",&ret);


[解决办法]
调用
hr = pDisp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparams, pvarRet, NULL, NULL);
[解决办法]
探讨
调用
hr = pDisp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparams, pvarRet, NULL, NULL);

[解决办法]
可以参考下面的代码
C/C++ code
HRESULT    CWebTool::GetJScript(CComPtr<IDispatch>& spDisp){    REGISTER_FUNC( CWebTool::GetJScript(CComPtr<IDispatch>& spDisp) );    HRESULT hr = E_FAIL;    CComPtr<IHTMLDocument2> spDoc;        try    {        VERIFY_SUCCEEDED( GetHtmlDocumentPtr( spDoc) );                VERIFY_SUCCEEDED(spDoc->get_Script( &spDisp ));        VERIFY_TRUE( spDisp != NULL );        hr = S_OK;    }    my_catch;    return hr;}HRESULT    CWebTool::CallJScript(const CString& strFunc, const CStringArray &paramArray, CComVariant *pVarResult){    REGISTER_FUNC( CWebTool::CallJScript(const CString strFunc, const CStringArray &paramArray, CComVariant *pVarResult) );    HRESULT hr = E_FAIL;    int i = 0;    CComPtr<IDispatch> spScript;    CComBSTR bstrMember(strFunc);    DISPID dispid = NULL;    const int arraySize = paramArray.GetSize();    DISPPARAMS dispparams;    EXCEPINFO excepInfo;    CComVariant vaResult;    UINT nArgErr = (UINT)-1;  // initialize to invalid arg    memset(&excepInfo, 0, sizeof excepInfo);    memset( &dispparams, 0, sizeof(dispparams) );    try    {        VERIFY_TRUE( arraySize >= 0 );        VERIFY_SUCCEEDED( GetJScript(spScript) );                VERIFY_SUCCEEDED( spScript->GetIDsOfNames( IID_NULL, &bstrMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid) );                dispparams.cArgs = arraySize;    // Number of arguments        dispparams.rgvarg = ( arraySize > 0 ) ? new VARIANT[dispparams.cArgs] : NULL;        for( i = 0; i < arraySize; i++ )        {            CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i); // back reading            bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);    //Array of arguments            dispparams.rgvarg[i].vt = VT_BSTR;        }        dispparams.cNamedArgs = 0;    // Number of named arguments            VERIFY_SUCCEEDED(spScript->Invoke( dispid, IID_NULL, 0, DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr) );            if(pVarResult)        {            *pVarResult = vaResult;        }        hr = S_OK;    }    my_catch;    for( i = 0; i < arraySize; i++)     {         // If false, the bstr argument is attached to the new object without making a copy by calling SysAllocString        //         _bstr_t _bstrVal(dispparams.rgvarg[i].bstrVal, FALSE );     }     delete []dispparams.rgvarg;    return hr;} 

热点排行