2010-01-04 24 views

risposta

3

Sì, è così che System.Windows.Forms.HtmlDocument.InvokeScript ottiene il valore di ritorno.

1

Ecco uno good article che mostra come eseguire questa operazione con C++.

Avanti è la mia interpretazione di esso. Dire, se il JavaScript lato client è:

function myMethod(name) 
{ 
    alert("Hello " + name); 

    return "Got it!"; 
} 

si può chiamare come tale:

CStringArray arrArgs; 
arrArgs.Add(L"John Smith"); 

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

Dove la funzione reale che si interfaccia con IHTMLWindow2 è questo:

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; 
}