Return value from execScript(IHTMLWindow2)

后端 未结 2 350
栀梦
栀梦 2020-12-21 01:31

What is the best way to get the return value from IHTMLWindow2::execScript?

I found this link, http://social.msdn.microsoft.com/Forums/en/ieextensiondevelopment/thre

相关标签:
2条回答
  • 2020-12-21 01:41

    Here's a good article that shows how to do this with C++.

    Next is my interpretation of it. Say, if your client-side JavaScript is:

    function myMethod(name)
    {
        alert("Hello " + name);
    
        return "Got it!";
    }
    

    You can call it as such:

    CStringArray arrArgs;
    arrArgs.Add(L"John Smith");
    
    CComVariant varRes;
    if(CallClientScript(L"myMethod", &arrArgs, &varRes))
    {
        if(varRes.vt == VT_BSTR)
            AfxMessageBox(varRes.bstrVal);
    }
    

    Where the actual function that interfaces with IHTMLWindow2 is this:

    BOOL CallClientScript(LPCTSTR pStrFuncName, CStringArray* pArrFuncArgs, CComVariant* pOutVarRes)
    {
        //Call client function in HTML
        //'pStrFuncName' = client script function name
        //'pArrFuncArgs' = if not NULL, list of arguments
        //'pOutVarRes' = if not NULL, will receive the return value
        //RETURN:
        //      = TRUE if done
        BOOL bRes = FALSE;
    
        CComVariant vaResult;
    
        CComPtr<IHTMLDocument2> pIDoc2;
        if(SUCCEEDED(this->GetDHtmlDocument(&pIDoc2)))  //Uses CDHtmlDialog as 'this'
        {
            //Getting IDispatch for Java Script objects
            CComPtr<IDispatch> spScript;
            if(SUCCEEDED(pIDoc2->get_Script(&spScript)))
            {
                //Find dispid for given function in the object
                CComBSTR bstrMember(pStrFuncName);
                DISPID dispid = NULL;
                if(SUCCEEDED(spScript->GetIDsOfNames(IID_NULL, &bstrMember, 1, LOCALE_USER_DEFAULT, &dispid)))
                {
    
                    const int arraySize = pArrFuncArgs ? pArrFuncArgs->GetSize() : 0;
    
                    //Putting parameters  
                    DISPPARAMS dispparams;
                    memset(&dispparams, 0, sizeof dispparams);
                    dispparams.cArgs      = arraySize;
                    dispparams.rgvarg     = new VARIANT[dispparams.cArgs];
                    dispparams.cNamedArgs = 0;
    
                    for( int i = 0; i < arraySize; i++)
                    {
                        CComBSTR bstr = pArrFuncArgs->GetAt(arraySize - 1 - i); // back reading
                        bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
                        dispparams.rgvarg[i].vt = VT_BSTR;
                    }
    
                    EXCEPINFO excepInfo;
                    memset(&excepInfo, 0, sizeof excepInfo);
                    UINT nArgErr = (UINT)-1;  // initialize to invalid arg
    
                    //Call JavaScript function         
                    if(SUCCEEDED(spScript->Invoke(dispid, IID_NULL, 0, DISPATCH_METHOD, &dispparams, &vaResult, &excepInfo, &nArgErr)))
                    {
                        //Done!
                        bRes = TRUE;
                    }
    
                    //Free mem
                    delete [] dispparams.rgvarg;
                }
            }
        }
    
        if(pOutVarRes)
            *pOutVarRes = vaResult;
    
        return bRes;
    }
    
    0 讨论(0)
  • 2020-12-21 01:42

    Yes, that's how System.Windows.Forms.HtmlDocument.InvokeScript gets the return value.

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