-05-05 14:17
1.在APP的InitInstance()函数中加入AfxEnableControlContainer();以支持ole控件;
2.在CDHtmlDialog::OnInitDialog()中加入SetExternalDispatch(GetIDispatch(TRUE));
3.通过以下函数实现调用js函数:
BOOL CDHtmlDialog::CallJScript(const CString strFunc, _variant_t* pVarResult)
{
CStringArray paramArray;
return CallJScript(strFunc,paramArray,pVarResult);
}
BOOL CDHtmlDialog::CallJScript(const CString strFunc,const CString strArg1,_variant_t* pVarResult)
{
CStringArray paramArray;
paramArray.Add(strArg1);
return CallJScript(strFunc,paramArray,pVarResult);
}
BOOL CDHtmlDialog::CallJScript(const CString strFunc, const CStringArray& paramArray,_variant_t* pVarResult)
{
CComPtr spScript;
if(!GetJScript(spScript))
{
MessageBox("函数GetJScrip调用失败!");
return FALSE;
}
CComBSTR bstrFunc(strFunc);
DISPID dispid = NULL;
HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrFunc,1,
LOCALE_SYSTEM_DEFAULT,&dispid);
if(FAILED(hr))
{
//MessageBox(GetSystemErrorMessage(hr));
return FALSE;
}
INT_PTR arraySize = paramArray.GetSize();
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = (UINT)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;
}
amedArgs = 0;
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof excepInfo);
_variant_t 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))
{
//MessageBox(GetSystemErrorMessage(hr));
return FALSE;
}
if(pVarResult)
{
*pVarResult = vaResult;
}
return TRUE;
}
BOOL CDHtmlDialog::GetJScript(CComPtr& spDisp)
{
if( m_spHtmlDoc==NULL )
return FALSE;
HRESULT hr = m_spHtmlDoc->get_Script(&spDisp);
ATLASSERT(SUCCEEDED(hr));
return SUCCEEDED(hr);
}
4.调用实例:
CallJScript("alert","test");