3,055
社区成员
发帖
与我相关
我的任务
分享
::CoInitialize(NULL); //初始化COM
HRESULT hr;
CComQIPtr <IHTMLDocument2> spDoc =(IHTMLDocument2*)GetHtmlDocument();//获取HTML文档
CComPtr<IHTMLFramesCollection2> spFramesCollection2;
spDoc->get_frames(&spFramesCollection2); //取得框架frame的集合
long nFrameCount=0; //取得子框架个数
hr=spFramesCollection2->get_length(&nFrameCount);
if (FAILED(hr)|| 0==nFrameCount) return;
for(long i=0; i<nFrameCount; i++)
{
CComVariant vDispWin2; //取得子框架的自动化接口
hr = spFramesCollection2->item(&CComVariant(i), &vDispWin2);
if (FAILED(hr)) continue;
CComQIPtr<IHTMLWindow2>spWin2 = vDispWin2.pdispVal;
if (!spWin2) return; //取得子框架的 IHTMLWindow2 接口
CComQIPtr <IHTMLDocument2> spDoc2;
spWin2->get_document(&spDoc2); //取得子框架的 IHTMLDocument2 接口
//接口指针获取之后都得进行判断是否为空
if(!spDoc2)
return;
//获取JS脚本 多个框架出问题
CComPtr<IHTMLElementCollection> spScripts;
//接口指针获取之后都得进行判断是否为空
hr=spDoc2->get_scripts(&spScripts);
if(!spScripts)
return;
IHTMLDocument2* pDoc = (IHTMLDocument2*)CHtmlView::GetHtmlDocument();;;
IHTMLWindow2* pHTMLWnd = NULL;
IHTMLDocument2* pFrameDoc = NULL;
IHTMLFramesCollection2* pFramesCollection = NULL;
LPDISPATCH lpDispatch;
long p;
VARIANT varindex,varresult;
varresult.vt=VT_DISPATCH;
varindex.vt=VT_I4;
if(pDoc!=NULL)
{
HRESULT hr=pDoc->get_frames(&pFramesCollection);
if(SUCCEEDED(hr)&&pFramesCollection!=NULL)
{
hr=pFramesCollection->get_length(&p);
if(SUCCEEDED(hr))
{
for(int i=0; i<p; i++)
{
varindex.lVal = i;
if(pFramesCollection->item(&varindex, &varresult)==S_OK)
{
lpDispatch=(LPDISPATCH)varresult.ppdispVal;
if (SUCCEEDED(lpDispatch-> QueryInterface(IID_IHTMLWindow2, (LPVOID*)&pHTMLWnd)))
{
if(SUCCEEDED(pHTMLWnd-> get_document(&pFrameDoc)))
{
//work with the pFrameDoc...
CComPtr<IDispatch> spScript=NULL;
hr=pFrameDoc->get_Script(&spScript);
CComBSTR bstrMember("tonclick");
DISPID dispid = NULL;
hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,LOCALE_SYSTEM_DEFAULT,&dispid);
if(SUCCEEDED(hr))
{
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = 1;//表示参数的计数
dispparams.rgvarg = new VARIANT[dispparams.cArgs];//表示对参数数组的引用
for( int i = 0; i < 1; i++)
{
CString param1="031105";
CComBSTR bstr_param1=param1.AllocSysString();
bstr_param1.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;
}
}
pHTMLWnd-> Release();
pHTMLWnd=NULL;
}
}
}
pFramesCollection-> Release();
}
}
pDoc-> Release();
}
这是我调用脚本的代码