最近关于vc++里面通过IDispatch的Invoke来实现和js的交互做点了tp,并且对以前不了解的“直接读取js变量值”和“传递多个参数”的部分重新实现了下。
1,调用js的函数,并传递多个参数
- CComQIPtr<IHTMLDocument2> pDoc = m_browser.get_Document();
- if (pDoc)
- {
- CComQIPtr<IDispatch> spDisp;
- HRESULT hr = pDoc->get_Script(&spDisp);
- if (SUCCEEDED(hr))
- {
- //要调用的函数名
- TCHAR* szMember = _T("setText");
- DISPID dispid;
- HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
- LOCALE_SYSTEM_DEFAULT, &dispid);
- if (SUCCEEDED(hr))
- {
- DISPPARAMS dispparams;
- memset(&dispparams, 0, sizeof dispparams);
- dispparams.cArgs = 2;
- //要传递的参数,注意强烈推荐用下面这个形式。传送string的时候,改为BSTR就可以。
- CComVariant* pvars = new CComVariant[2];
- pvars[1] = L"1";
- pvars[0] = L"2";
- pvars[0].ChangeType(VT_I4);
- pvars[1].ChangeType(VT_I4);
- dispparams.rgvarg = pvars;
- dispparams.cNamedArgs = 0;
- EXCEPINFO excepInfo;
- memset(&excepInfo, 0, sizeof(excepInfo));
- CComVariant vaResult;
- UINT nArgErr = (UINT)-1; // initialize to invalid arg
- hr = spDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
- &dispparams,
- &vaResult,
- &excepInfo,
- &nArgErr);
- }
- }
- }
CComQIPtr<IHTMLDocument2> pDoc = m_browser.get_Document();
if (pDoc)
{
CComQIPtr<IDispatch> spDisp;
HRESULT hr = pDoc->get_Script(&spDisp);
if (SUCCEEDED(hr))
{
//要调用的函数名
TCHAR* szMember = _T("setText");
DISPID dispid;
HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispid);
if (SUCCEEDED(hr))
{
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = 2;
//要传递的参数,注意强烈推荐用下面这个形式。传送string的时候,改为BSTR就可以。
CComVariant* pvars = new CComVariant[2];
pvars[1] = L"1";
pvars[0] = L"2";
pvars[0].ChangeType(VT_I4);
pvars[1].ChangeType(VT_I4);
dispparams.rgvarg = pvars;
dispparams.cNamedArgs = 0;
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof(excepInfo));
CComVariant vaResult;
UINT nArgErr = (UINT)-1; // initialize to invalid arg
hr = spDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
&dispparams,
&vaResult,
&excepInfo,
&nArgErr);
}
}
}
2,直接读取js的变量(不是通过对某个控件赋值再读取的形式)
- CComQIPtr<IHTMLDocument2> pDoc = m_browser.get_Document();
- if (pDoc)
- {
- CComQIPtr<IDispatch> spDisp;
- HRESULT hr = pDoc->get_Script(&spDisp);
- if (SUCCEEDED(hr))
- {
- //js中的变量名字
- TCHAR* szMember = _T("nItem");
- DISPID dispid;
- HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
- LOCALE_SYSTEM_DEFAULT, &dispid);
- if (SUCCEEDED(hr))
- {
- DISPPARAMS dispparams;
- memset(&dispparams, 0, sizeof dispparams);
- EXCEPINFO excepInfo;
- memset(&excepInfo, 0, sizeof(excepInfo));
- CComVariant vaResult;
- UINT nArgErr = (UINT)-1; // initialize to invalid arg
- hr = spDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
- DISPATCH_PROPERTYGET,
- &dispparams,
- &vaResult,
- &excepInfo,
- &nArgErr);
- }
- }
- }
CComQIPtr<IHTMLDocument2> pDoc = m_browser.get_Document();
if (pDoc)
{
CComQIPtr<IDispatch> spDisp;
HRESULT hr = pDoc->get_Script(&spDisp);
if (SUCCEEDED(hr))
{
//js中的变量名字
TCHAR* szMember = _T("nItem");
DISPID dispid;
HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispid);
if (SUCCEEDED(hr))
{
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof(excepInfo));
CComVariant vaResult;
UINT nArgErr = (UINT)-1; // initialize to invalid arg
hr = spDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYGET,
&dispparams,
&vaResult,
&excepInfo,
&nArgErr);
}
}
}