How to clear web browser management

I use ATL in Visual C ++ 10 to control the browser. My code is similar to this example: http://msdn.microsoft.com/en-us/library/9d0714y1(v=vs.80).aspx

The difference is that I have a main window, and then the child window contains a browser control. After 2 minutes, I need to close the browser, completely kill the activeX browser, but this child window should be alive and do something else. But somehow this browser control is still there, I can see the scroll bars or something like that.

I also tried to create a child window to an existing child window, and when I close the browser I will destroy this child of the child, but still it does not work!

Here is how I close:

CLOSE() { m_spIWebBrowser2->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty); m_spIWebBrowser2->Stop(); m_spIWebBrowser2->put_Visible(VARIANT_FALSE); m_spIWebBrowser2->Quit(); DestroyWindow(m_wndChild.m_hWnd); } 

Thanks!

+4
source share
3 answers

I had a lot of “access violation” problems when closing the web browser control, these were the steps that worked for me:

  • Prevent any previously described events (DWebBrowserEvents2 in my case).
  • If you enabled click events, disable them as follows: _variant_t v; v.vt = VT_DISPATCH; v.pdispVal = 0; IHTMLDocument2->put_onclick(v); _variant_t v; v.vt = VT_DISPATCH; v.pdispVal = 0; IHTMLDocument2->put_onclick(v);
  • IWebBrowser2->Stop()
  • IWebBrowser2->ExecWB(OLECMDID_CLOSE, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0) - when closing the browser window through window.external.CloseWindow () I had unhandled exceptions and OLECMDID_CLOSE fixed it.
  • IWebBrowser2->put_Visible(VARIANT_FALSE)
  • IWebBrowser2->Release()
  • IOleInPlaceObject->InPlaceDeactivate()
  • IOleInPlaceObject->Release()
  • IOleObject->DoVerb(OLEIVERB_HIDE, NULL, IOleClientSite, 0, windowHandle_, NULL)
  • IOleObject->Close(OLECLOSE_NOSAVE)
  • OleSetContainedObject(IOleObject, FALSE)
  • IOleObject->SetClientSite(NULL)
  • CoDisconnectObject(IOleObject, 0)
  • IOleObject->Release()

IWebBrowser2->Quit() should not be called for the WebBrowser control (CLSID_WebBrowser), it is intended only for the Internet Explorer object (CLSID_InternetExplorer).

Why is it so hard?

+8
source

My experience is that some calls may require message processing to work properly. Try translating some messages between your calls to Navigate , Stop , etc. When working with web browser interfaces, I PostMessage I often run the next step to make sure that the previous step managed to finish.

The problem may be related to your child thread. You cannot access web browser interfaces between threads without extra work. COM needs to be initialized as a single-threaded apartment (STA). And you need to follow the STA rules :

  • Each object must live on only one thread (in a single-threaded apartment). Initialize the COM library for each thread.
  • Marshal all points to objects when transferring between apartments.
  • Each single-threaded apartment must have a message loop for processing calls from other processes and apartments within the same process. Single-threaded apartments without objects (only for clients) also need a message loop to send broadcast messages that are used by some applications.
  • ...
0
source

If I use DialogBox and drop IEControl as a resource on it, and DialogBox comes from CAxDialogImpl <> - then when I call the DestroyWindow () of the dialog box, it automatically performs cleaning () - this is what I need. But initially I wanted to get rid of DialogBox itself and use IEControl directly in my window, it seems, not ..

0
source

Source: https://habr.com/ru/post/1383124/


All Articles