How to Cleanly Destroy WebBrowser Control

前端 未结 3 454
夕颜
夕颜 2021-02-04 21:09

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

3条回答
  •  无人及你
    2021-02-04 21:54

    I had many problems with "access violation" when closing webbrowser control, these are the steps that worked for me:

    1. Unadvise any previously advised events (DWebBrowserEvents2 in my case).
    2. If you've attached click events unattach them like this: _variant_t v; v.vt = VT_DISPATCH; v.pdispVal = 0; IHTMLDocument2->put_onclick(v);
    3. IWebBrowser2->Stop()
    4. IWebBrowser2->ExecWB(OLECMDID_CLOSE, OLECMDEXECOPT_DONTPROMPTUSER, 0, 0) - when closing browser window through window.external.CloseWindow() I had unhandled exceptions and OLECMDID_CLOSE fixed it.
    5. IWebBrowser2->put_Visible(VARIANT_FALSE)
    6. IWebBrowser2->Release()
    7. IOleInPlaceObject->InPlaceDeactivate()
    8. IOleInPlaceObject->Release()
    9. IOleObject->DoVerb(OLEIVERB_HIDE, NULL, IOleClientSite, 0, windowHandle_, NULL)
    10. IOleObject->Close(OLECLOSE_NOSAVE)
    11. OleSetContainedObject(IOleObject, FALSE)
    12. IOleObject->SetClientSite(NULL)
    13. CoDisconnectObject(IOleObject, 0)
    14. IOleObject->Release()

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

    Why must it be so hard?

提交回复
热议问题