// 定义 CWebBrowser2
Code
class CXXXDlg
{
CWebBrowser2 m_theWebBrowser;
};
// 装载包含JavaScript的Html
Code
BOOL CXXXDlg::OnInitDialog()
{
m_theWebBrowser.Navigate2(varURL, varEmpty,varEmpty,varEmpty,varEmpty);
}
// 调用JavaScript
Code
void CXXXDlg::OnBtnClickedButton1()
{
CString strJsName = _T("testArg1");
CString strJsParam = _T("This is a test for call JavaScript from C++")
CallJScript(strJsName , strJsParam );
}
// C++调用JavaScript
Code
bool CXXXDlg::GetJScript(CComPtr<IDispatch>& spDisp)
{
CComPtr<IHTMLDocument2> pHtmlDoc=NULL;
CComPtr<IDispatch> spDocDisp;
HRESULT hr;
if(spDisp == NULL)
{
spDocDisp = m_theWebBrowser.GetDocument();
if(spDocDisp)
{
hr = spDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc);
if (SUCCEEDED(hr) && pHtmlDoc)
{
// get script
hr = pHtmlDoc->get_Script(&spDisp);
ATLASSERT(SUCCEEDED(hr));
}
}
}
ATLASSERT(SUCCEEDED(hr));
return SUCCEEDED(hr);
}
// bool CallJScript(const CString strFunc,CComVariant* pVarResult = NULL);
bool CXXXDlg::CallJScript(const CString strFunc,CComVariant* pVarResult)
{
CStringArray paramArray;
return CallJScript(strFunc, paramArray, pVarResult);
}
// bool CallJScript(const CString strFunc,const CString strArg1,CComVariant* pVarResult = NULL);
bool CXXXDlg::CallJScript(const CString strFunc,const CString strArg1,CComVariant* pVarResult)
{
CStringArray paramArray;
paramArray.Add(strArg1);
return CallJScript(strFunc,paramArray,pVarResult);
}
const CString GetSystemErrorMessage(DWORD dwError)
{
CString strError;
LPTSTR lpBuffer;
if(!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
(LPTSTR) &lpBuffer, 0, NULL))
{
strError = _T("FormatMessage Netive Error") ;
}
else
{
strError = lpBuffer;
LocalFree(lpBuffer);
}
return strError;
}
// bool CallJScript(const CString strFunc,const CStringArray& paramArray,CComVariant* pVarResult = NULL);
bool CXXXDlg::CallJScript(const CString strFunc, const CStringArray& paramArray,CComVariant* pVarResult)
{
CComPtr<IDispatch> spScript=NULL;
if(!GetJScript(spScript))
{
AfxMessageBox (_T("Cannot GetScript"));
return false;
}
CComBSTR bstrMember(strFunc);
DISPID dispid = NULL;
HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,
LOCALE_SYSTEM_DEFAULT,&dispid);
if(FAILED(hr))
{
AfxMessageBox(GetSystemErrorMessage(hr));
return false;
}
const int arraySize = paramArray.GetSize();
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = arraySize;
dispparams.rgvarg = new VARIANT[dispparams.cArgs];
for( int i = 0; i < arraySize; i++)
{
CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i); // back reading
bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
dispparams.rgvarg[i].vt = VT_BSTR;
}
dispparams.cNamedArgs = 0;
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
CComVariant vaResult;
UINT nArgErr = (UINT)-1; // initialize to invalid arg
hr = spScript->Invoke(dispid,IID_NULL,0,
DISPATCH_METHOD,&dispparams,&vaResult,&excepInfo,&nArgErr);
delete [] dispparams.rgvarg;
if(FAILED(hr))
{
AfxMessageBox(GetSystemErrorMessage(hr));
return false;
}
if(pVarResult)
{
*pVarResult = vaResult;
}
return true;
}
////////////////////////////////////////////////////////////
// 简洁改进
HRESULT CXXUtil::CallJsFunc(const CComPtr<IHTMLDocument2> spDocument2,
const CString &strJsFuncName, const std::list<CString> ¶ms, 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;
}