以下是程序片段:
CComPtr<IShellWindows> psw;
psw.CoCreateInstance(CLSID_ShellWindows);
if(psw)
{
CDWordArray arHWNDShellWindows;
CTypedPtrArray<CPtrArray,CComQIPtrIWebBrowser2*> arShellWindows;//
long lShellWindowCount=0;
psw->get_Count(&lShellWindowCount);
for(long i=0;i<lShellWindowCount;i++)
{
CComPtr<IDispatch> pdispShellWindow;
psw->Item(COleVariant(i),&pdispShellWindow);
CComQIPtr<IWebBrowser2> pIE(pdispShellWindow);
if(pIE)
{
CString strWindowClass=GetWindowClassName(pIE);
if(strWindowClass==_T("IEFrame"))
{
HWND hWndID=NULL;
pIE->get_HWND((long*)&hWndID);
arHWNDShellWindows.Add((DWORD)hWndID);
arShellWindows.Add(new CComQIPtrIWebBrowser2(pIE));
}
}
}
CString s;
s.Format(_T("%d"),arHWNDShellWindows.GetSize());
if(arHWNDShellWindows.GetSize()>0)//at least one shell window found
//, get the top one in z order
{
//the first top-level window in zorder
HWND hwndTest=::GetWindow((HWND)arHWNDShellWindows[0],GW_HWNDFIRST);
DWORD m_dwCookie;
while( hwndTest)
{
for(int i=0;i<arHWNDShellWindows.GetSize();i++)
{
if(hwndTest==(HWND)arHWNDShellWindows[i])
{
LPUNKNOWN pUnkSink = GetIDispatch(FALSE);
BOOL badv=AfxConnectionAdvise((LPUNKNOWN)*arShellWindows[i],DIID_DWebBrowserEvents2,this->GetInterface(&IID_IUnknown),TRUE,&m_dwCookie); //连接到IE事件.
CString s;
s.Format(_T("%s"),badv?_T("成功"):_T("失败"));
m_pParent->AddEventToList(s);
break;
}
}
hwndTest = ::GetWindow(hwndTest, GW_HWNDNEXT);
}
}
for(int i=0;i<arShellWindows.GetSize();i++)
{
delete arShellWindows[i];
}
}程序能够监听IE事件,可是关闭一个IE时,程序就崩溃了,请问是不是缺了什么!
CComPtr<IShellWindows> psw;
psw.CoCreateInstance(CLSID_ShellWindows);
if(psw)
{
CDWordArray arHWNDShellWindows;
CTypedPtrArray<CPtrArray,CComQIPtrIWebBrowser2*> arShellWindows;//
long lShellWindowCount=0;
psw->get_Count(&lShellWindowCount);
for(long i=0;i<lShellWindowCount;i++)
{
CComPtr<IDispatch> pdispShellWindow;
psw->Item(COleVariant(i),&pdispShellWindow);
CComQIPtr<IWebBrowser2> pIE(pdispShellWindow);
if(pIE)
{
CString strWindowClass=GetWindowClassName(pIE);
if(strWindowClass==_T("IEFrame"))
{
HWND hWndID=NULL;
pIE->get_HWND((long*)&hWndID);
arHWNDShellWindows.Add((DWORD)hWndID);
arShellWindows.Add(new CComQIPtrIWebBrowser2(pIE));
}
}
}
CString s;
s.Format(_T("%d"),arHWNDShellWindows.GetSize());
if(arHWNDShellWindows.GetSize()>0)//at least one shell window found
//, get the top one in z order
{
//the first top-level window in zorder
HWND hwndTest=::GetWindow((HWND)arHWNDShellWindows[0],GW_HWNDFIRST);
DWORD m_dwCookie;
while( hwndTest)
{
for(int i=0;i<arHWNDShellWindows.GetSize();i++)
{
if(hwndTest==(HWND)arHWNDShellWindows[i])
{
LPUNKNOWN pUnkSink = GetIDispatch(FALSE);
BOOL badv=AfxConnectionAdvise((LPUNKNOWN)*arShellWindows[i],DIID_DWebBrowserEvents2,this->GetInterface(&IID_IUnknown),TRUE,&m_dwCookie); //连接到IE事件.
CString s;
s.Format(_T("%s"),badv?_T("成功"):_T("失败"));
m_pParent->AddEventToList(s);
break;
}
}
hwndTest = ::GetWindow(hwndTest, GW_HWNDNEXT);
}
}
for(int i=0;i<arShellWindows.GetSize();i++)
{
delete arShellWindows[i];
}
}程序能够监听IE事件,可是关闭一个IE时,程序就崩溃了,请问是不是缺了什么!
解决方案 »
- UDP 接收高速数据
- 状态栏显示鼠标当前的坐标
- ---web服务器接收到的GET请求,是否一定要ascii编码?
- 一个很简单的问题,让我郁闷死了!
- 因公司转型做其它方面的产品,本人出售KTV的点歌软件的代码
- link出问题啦
- 模板类的问题,感兴趣的过来帮我看看,谢谢了!
- 请问:我用openprocess打开dllhost.exe进程怎么返回失败!!!我要杀掉该进程该如何做呢?
- 请教ADO的GetCollect返回
- dispatchmessage函数的作用,以及产生的消息是如何与某个窗口产生联系的
- 求救:程序中引用了CComQIPtrIWebBrowser2,在IE关闭时,程序崩溃!
- QQ被盗散分解闷~~~
可是我连接了所有的IE窗体,在IE的关闭事件OnQuit()中并没有相应的窗体对象,所以就不知道是哪一个IE窗体关闭了,从而也就没办法使用AfxConnectionUnAdvise了,请问大家有办法吗?
在Invoke中的DispID = 253时执行~~~~CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
这是自动释放COM指针,一有这样的代码就会导致IE自动关闭。
(具体原因我也还没搞清楚)CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
我监听的IE事件中有OnQuit()事件,可是没有参数,不知是哪个IE窗体关闭了.
你说的"在Invoke中的DispID = 253时执行~~~~"具体在哪个方法里面执行呢?另外不使用CComQIPtr,上面的代码段有其他方法实现吗?