I'm trying to use the OnQuit Event of IE, but it's just not firing when I close Internet Explorer. Is there any other way to detect closing of ta开发者_运维技巧bs, or the browser in IE?
I'm using it in a BHO written in C#.
Using IE9 and native c++ BHO (ATL), I've no problems getting the onQuit event My BHO derives from IDispEventImpl, and in the SINK_MAP I specify the DISPID_ONQUIT event:
class ATL_NO_VTABLE CMyBho
:public CComObjectRootEx<CComSingleThreadModel>
...
,IObjectWithSiteImpl<CMyBho>
,IDispEventImpl<1, CMyBho, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
{
...
BEGIN_SINK_MAP(CMyBho)
SINK_ENTRY_EX( 1, DIID_DWebBrowserEvents2, DISPID_ONQUIT, onQuit )
END_SINK_MAP()
...
STDMETHODCALLTYPE onQuit( );
...
STDMETHOD(SetSite)(IUnknown* unkSite) {
CComQIPtr<IServiceProvider> ifServiceProvider(unkSite);
CComPtr<IWebBrowser2> ifBrz2;
ifServiceProvider->QueryService( SID_SWebBrowserAPP, IID_IWebBrowser2,
(void**)&ifBrz2 );
this->DispEventAdvise( ifBrz2 );
}
}
Saying all that, I know this is native code (vs. C#) and this is IE9 - but maybe it will give you a hint what needs to be done on your C# implementation. Sent me a note or comment if you want the full source code or need more help.
If you are using WebBrowser Object then as per this OnQuit() MSDN:
The WebBrowser object ignores this event.
. One solution is to use native code as mentioned in Uri's answer.
OnQuit works pretty well for me with IE11.
Handler:
public void OnQuit()
{
logger.Debug("Entered OnQuit");
}
Wiring:
int IObjectWithSite.SetSite(object site)
{
if (site != null)
{
mainWindowBrowser = (WebBrowser)site;
mainWindowBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
mainWindowBrowser.OnQuit += new DWebBrowserEvents2_OnQuitEventHandler(this.OnQuit);
}
else
{
mainWindowBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
mainWindowBrowser.OnQuit -= new DWebBrowserEvents2_OnQuitEventHandler(this.OnQuit);
mainWindowBrowser = null;
}
return 0;
}
精彩评论