主要函数 CallJScript BOOL CallJScript(const wchar_t * func, VARIANT * vtParams, int cNumberOfParam, VARIANT * vtResult) { if(!func || !func[0]) return FALSE; CComPtr<IDispatch> spScript; spScript = GetJScript(); if(!spScript) return FALSE; DISPID dispID = NULL; CComBSTR bstrFunc(func); HRESULT hr = spScript->GetIDsOfNames(IID_NULL, &bstrFunc, 1, LOCALE_SYSTEM_DEFAULT, &dispID); if(FAILED(hr)) return FALSE; DISPPARAMS dispParams = {0}; dispParams.cArgs = cNumberOfParam; dispParams.rgvarg = new VARIANT[cNumberOfParam]; for(int i = 0; i < cNumberOfParam; ++i) { VariantInit(&dispParams.rgvarg[i]); VariantCopy(&dispParams.rgvarg[i], &vtParams[cNumberOfParam - 1 - i]); } hr = spScript->Invoke(dispID, IID_NULL, 0, DISPATCH_METHOD, &dispParams, vtResult, NULL, NULL); delete [] dispParams.rgvarg; if(FAILED(hr)) return FALSE; return TRUE; } 其它函数 IHTMLDocument2 * GetIDocument2() { if(!m_pWebBrowser2) return NULL; IDispatch *pIDispatch = NULL; if (SUCCEEDED(m_pWebBrowser2->get_Document(&pIDispatch)) && pIDispatch) { IHTMLDocument2 *pIDocument2 = NULL; if (SUCCEEDED(pIDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&pIDocument2))) { pIDispatch->Release(); return pIDocument2; } pIDispatch->Release(); } return NULL; } IDispatch * GetJScript() { CComPtr<IHTMLDocument2> doc; doc = GetIDocument2(); if(!doc) return NULL; IDispatch * dsScript = NULL; HRESULT hr = doc->get_Script(&dsScript); if(SUCCEEDED(hr) && dsScript) return dsScript; else return NULL; }