Azure/C++/C#/Java/Asp.net/JavaScript/Html - MFC/COM/ActiveX/eclipse/Web

dlbrant

working soft

博客园 首页 新随笔 管理

 

// 定义 CWebBrowser2

Code

 // 装载包含JavaScript的Html

Code

 // 调用JavaScript

Code

 // C++调用JavaScript

Code

 

 ////////////////////////////////////////////////////////////

// 简洁改进

HRESULT CXXUtil::CallJsFunc(const CComPtr<IHTMLDocument2> spDocument2, 
                                    const CString &strJsFuncName, const std::list<CString> &params, CComVariant *pVarResult)
{
    HRESULT hr = S_FALSE;

    if (spDocument2)
    {
        CComPtr<IDispatch> spScript = NULL;
        hr = spDocument2->get_Script(&spScript);

        if (SUCCEEDED(hr) && spScript)
        {
            CComBSTR bstrFunc(strJsFuncName);
            DISPID dispid = 0;
            hr = spScript->GetIDsOfNames(IID_NULL, &bstrFunc, 1, LOCALE_SYSTEM_DEFAULT, &dispid);

            if(SUCCEEDED(hr))
            {
                const size_t paramCnt = params.size();
                DISPPARAMS dispparams = {0};
                //memset(&dispparams, 0, sizeof dispparams);
                dispparams.cArgs = paramCnt;
                dispparams.cNamedArgs = 0;
                dispparams.rgvarg = new VARIANT[dispparams.cArgs];

                for(size_t i = 0; i < paramCnt; i++)
                {
                    std::list<CString>::const_iterator cit = params.begin();
                    std::advance(cit, (paramCnt - 1 - i));// back reading
                    CComBSTR bstrParam = (*cit).GetString();
                    dispparams.rgvarg[i].vt = VT_BSTR;
                    dispparams.rgvarg[i].bstrVal = bstrParam.Detach();
                }

                EXCEPINFO excepInfo = {0};
                //memset(&excepInfo, 0, sizeof excepInfo);
                CComVariant vaResult;
                UINT nArgErr = (UINT)0; //-1;  // initialize to invalid arg

                hr = spScript->Invoke(dispid, IID_NULL, 0 , DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr);

                // cleanup
                for(UINT ix = 0; ix < dispparams.cArgs; ++ix)
                {
                    ::SysFreeString(dispparams.rgvarg[ix].bstrVal);
                }
                delete [] dispparams.rgvarg;

                if(SUCCEEDED(hr) && pVarResult)
                {
                    *pVarResult = vaResult;
                }
            }
        }
    }

    return hr;
}

 

 

 

 

 

 

 

 

 

 

posted on 2009-02-16 13:50  dlbrant  阅读(5372)  评论(0编辑  收藏  举报

Software