以下是程序片段:
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时,程序就崩溃了,请问是不是缺了什么!

解决方案 »

  1.   

    对AfxConnectionAdvise事件,是不是在IE关闭时需要一个AfxConnectionUnAdvise!
    可是我连接了所有的IE窗体,在IE的关闭事件OnQuit()中并没有相应的窗体对象,所以就不知道是哪一个IE窗体关闭了,从而也就没办法使用AfxConnectionUnAdvise了,请问大家有办法吗?
      

  2.   

    需要AfxConnectionUnAdvise, 你连接了一个,就要UnAdvise几个.
    在Invoke中的DispID = 253时执行~~~~CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
      

  3.   

    还有不要使用CComQIPtr....
    这是自动释放COM指针,一有这样的代码就会导致IE自动关闭。
    (具体原因我也还没搞清楚)CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
      

  4.   

    CoolSlob() ( ) :谢谢,请问:
    我监听的IE事件中有OnQuit()事件,可是没有参数,不知是哪个IE窗体关闭了.
    你说的"在Invoke中的DispID = 253时执行~~~~"具体在哪个方法里面执行呢?另外不使用CComQIPtr,上面的代码段有其他方法实现吗?