{"id":2600,"date":"2022-08-30T15:26:13","date_gmt":"2022-08-30T15:26:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/how-to-cleanly-destroy-webbrowser-control-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:13","modified_gmt":"2022-08-30T15:26:13","slug":"how-to-cleanly-destroy-webbrowser-control-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-cleanly-destroy-webbrowser-control-collection-of-common-programming-errors\/","title":{"rendered":"How to Cleanly Destroy WebBrowser Control-Collection of common programming errors"},"content":{"rendered":"<p>I am using ATL in VisualC++10 to host browser control. My code is similar to this example: http:\/\/msdn.microsoft.com\/en-us\/library\/9d0714y1(v=vs.80).aspx<\/p>\n<p>Difference is I have main window and then child window hosts the browser control. After 2 minutes i have to close the browser completely kill the browser activeX but this child window should be alive and do something else. But somehow this browser control still stays there, i can either see scrollbars or something..<\/p>\n<p>I have also tried by creating child window to an existing child window, and at the time of closing browser I then destroy this child of a child &#8211; but still it does not work!<\/p>\n<p>This is how I am closing:<\/p>\n<pre><code>CLOSE()\n{\n    m_spIWebBrowser2-&gt;Navigate(bstrURL, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty);\n    m_spIWebBrowser2-&gt;Stop();\n    m_spIWebBrowser2-&gt;put_Visible(VARIANT_FALSE);\n    m_spIWebBrowser2-&gt;Quit();\n    DestroyWindow(m_wndChild.m_hWnd);\n}\n<\/code><\/pre>\n<p>Thanks!<\/p>\n<ol>\n<li>\n<p>I had many problems with &#8220;access violation&#8221; when closing webbrowser control, these are the steps that worked for me:<\/p>\n<ol>\n<li>Unadvise any previously advised events (DWebBrowserEvents2 in my case).<\/li>\n<li>If you&#8217;ve attached click events unattach them like this: <code>_variant_t v; v.vt = VT_DISPATCH; v.pdispVal = 0; IHTMLDocument2-&gt;put_onclick(v);<\/code><\/li>\n<li><code>IWebBrowser2-&gt;Stop()<\/code><\/li>\n<li><code>IWebBrowser2-&gt;ExecWB(OLECMDID_CLOSE, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0)<\/code> &#8211; when closing browser window through window.external.CloseWindow() I had unhandled exceptions and OLECMDID_CLOSE fixed it.<\/li>\n<li><code>IWebBrowser2-&gt;put_Visible(VARIANT_FALSE)<\/code><\/li>\n<li><code>IWebBrowser2-&gt;Release()<\/code><\/li>\n<li><code>IOleInPlaceObject-&gt;InPlaceDeactivate()<\/code><\/li>\n<li><code>IOleInPlaceObject-&gt;Release()<\/code><\/li>\n<li><code>IOleObject-&gt;DoVerb(OLEIVERB_HIDE, NULL, IOleClientSite, 0, windowHandle_, NULL)<\/code><\/li>\n<li><code>IOleObject-&gt;Close(OLECLOSE_NOSAVE)<\/code><\/li>\n<li><code>OleSetContainedObject(IOleObject, FALSE)<\/code><\/li>\n<li><code>IOleObject-&gt;SetClientSite(NULL)<\/code><\/li>\n<li><code>CoDisconnectObject(IOleObject, 0)<\/code><\/li>\n<li><code>IOleObject-&gt;Release()<\/code><\/li>\n<\/ol>\n<p><code>IWebBrowser2-&gt;Quit()<\/code> should not be called for WebBrowser control (CLSID_WebBrowser), it is intended only for Internet Explorer object (CLSID_InternetExplorer).<\/p>\n<p>Why must it be so hard?<\/p>\n<\/li>\n<li>\n<p>My experience is that some calls might need message processing to function properly. Try to pump some messages between your calls to <code>Navigate<\/code>, <code>Stop<\/code> etc. When working with the web browser interfaces I <code>PostMessage<\/code> myself often to trigger the next step to make sure the previous step had time to complete.<\/p>\n<p>The problem might be related to your child thread. You cannot access web browser interfaces between threads without some additional work. COM needs to be initialized as single-threaded apartment (STA). And you need to follow the rules of STAs:<\/p>\n<ul>\n<li>Every object should live on only one thread (within a single-threaded apartment). Initialize the COM library for each thread.<\/li>\n<li><strong>Marshal all pointers to objects when passing them between apartments.<\/strong><\/li>\n<li><strong>Each single-threaded apartment must have a message loop<\/strong> to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch the broadcast messages that some applications use.<\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>If I use DialogBox and drop a IEControl on it as a resource and DialogBox is derived from CAxDialogImpl &#8211; then while I call DestroyWindow() of dialogBox then it is automatically doing the cleanup() &#8211; which is what I required. But originally I wanted to get rid of DialogBox itself and use IEControl directly on my Window, it seems not..<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 02:41:50. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am using ATL in VisualC++10 to host browser control. My code is similar to this example: http:\/\/msdn.microsoft.com\/en-us\/library\/9d0714y1(v=vs.80).aspx Difference is I have main window and then child window hosts the browser control. After 2 minutes i have to close the browser completely kill the browser activeX but this child window should be alive and do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2600","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2600"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2600\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}