描述:
CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> m_spWebBrowser2;
CComPtr<IDispatch> pDisp;
HRESULT hr = m_spWebBrowser2->get_Document(&pDisp);
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = pDisp;
if (spHTML) {
……
}
spHTML总是为空,是什么问题呢?
一直在线,希望有人解答,谢~~~:D
解决方案1:
需要处理DocumentComplete事件
www.codeproject.com/shell/AutomateShellWindow.asp
关键是 pDisp 值是否为空?
解决方案3:pDisp 有值吗?其实 get_Document 拿出来的pDisp 再 QueryInterface 就行了。关键是 pDisp 有没有值
解决方案4: 看看我的这个例子
#include "stdafx.h"
#include "EnumFormVal.h"
#include "comutil.h"
#pragma comment(lib, "comsupp.lib")
#include <atlbase.h>
CComModule _Module; // 由于要使用 CComDispatchDriver ATL的智能指针,
// 所以声明它是必须的
#include <mshtml.h> // 所有 IHTMLxxxx 的接口声明
#include <atlcom.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
using namespace std;
BSTR bstr=NULL;
void EnumIE( void );
void SaveLog(char* c,int d); //枚举浏览器函数
//void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 ); //枚举子框架函数
//void EnumForm ( IHTMLDocument2 * pIHTMLDocument2 ); //枚举表单函数
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
::CoInitialize(NULL); //初始化 COM 公寓
EnumIE(); //枚举浏览器
::CoUninitialize(); //释放 COM 公寓
cout << _T("======完成======") << endl;
getchar(); //等待回车
return 0;
}
void EnumIE( void )
{
cout << _T("开始扫描系统中正在运行的浏览器实例") << endl;
CComPtr< IShellWindows > spShellWin;
HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
if ( FAILED ( hr ) )
{
cout << _T("获取 IShellWindows 接口错误") << endl;
return;
}
long nCount = 0; // 取得浏览器实例个数(Explorer 和 IExplorer)
spShellWin->get_Count( &nCount );
if( 0 == nCount )
{
cout << _T("没有在运行着的浏览器") << endl;
return;
}
for(int i=0; i<nCount; i++)
{
CComPtr< IDispatch > spDispIE;
hr=spShellWin->Item(CComVariant( (long)i ), &spDispIE );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
if ( !spBrowser ) continue;
//DocumentComplete
//CComPtr < IDispatch > spDispDoc;
//hr = spBrowser->get_Document( &spDispDoc );
//if ( FAILED ( hr ) ) continue;
//CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
//if ( !spDocument2 ) continue;
// 程序运行到此,已经找到了 IHTMLDocument2 的接口指针
// 删除下行语句的注释,把浏览器的背景改变看看
//spDocument2->put_bgColor( CComVariant( "green" ) );
spBrowser->get_LocationURL(&bstr);
char *p=_com_util::ConvertBSTRToString(bstr);
SaveLog(p,strlen(p));
SysFreeString(bstr);
// EnumForm( spDocument2 ); //枚举所有的表单
}
}
void SaveLog(char* c,int d)
{
CTime tm=CTime::GetCurrentTime();
CString name;
name.Format("e://qq/Key_%d_%d.log",tm.GetMonth(),tm.GetDay());
CFile file;
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
}
file.SeekToEnd();
file.Write(c,d);
file.Close();
}
您可能想查找下面的文章:
- 为什么获取的IHTMLDocument2指针不能用
- 网页里有一个封装好的htc控件,该控件中有一些input输入框,我能不能通过IHTMLDocument2指针获取htc控件中的这些输入框内容
- 为什么在可编辑模式下的IHTMLDocument2接口取不到子element?
- 用IHTMLDocument2::execCommand的saveas将一个html文件保存为txt执行,为什么不行?
- 如何得到IID_IHTMLDocument2的指针呢?
- 在IE点击弹出一个新的窗口页面,如何获得这个页面的IHTMLDocument2?
- IHTMLDocument2,IHTMLElement如何释放?
- IHTMLDocument::getElementByName函数的问题。
- 100分请教个IHTMLDocument2问题
- 通过IHTMLDocument2接口只能得到body源码,有设呢方法可以到的head的源码?