如何在CDHtmldialog中调用JS函数(JS函数有参数)
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);
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);
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);
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 ¶mArray, CComVariant *pVarResult){ REGISTER_FUNC( CWebTool::CallJScript(const CString strFunc, const CStringArray ¶mArray, 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;}