c++builderrad-studioc++builder-2010

No DefaultInterface in TCppWebBrowser


I have a problem with the web browser memory leak - which I've found here: How can I hide WebBrowser till the website complete the loading / download process?

The solution is to use browser.DefaultInterface.Document instead of browser.Document.

I was using this:

DelphiInterface<IHTMLDocument2> diDoc = browser->Document;

But I am using TCppWebBrowser and it only has browser->DefaultDispatch doesn't have DefaultInterface like TWebBrowser which I assume is the same thing but I don't know how to query TCppWebBrowser for IHTMLDocument2 to avoid the memory leak, until I fully switch to newer version of RAD Studio where the memory leak has been fixed (Sydney, currently on 2010).

I tried this:

DelphiInterface<IHTMLDocument2> diDoc;
browser->DefaultDispatch->QueryInterface(IID_IHTMLDocument2, &diDoc);

But that doesn't seem to work.

This works... but...

DelphiInterface<IHTMLDocument2> diDoc = browser->DefaultDispatch;

But the resulting diDoc is NULL. (it is not NULL for browser->Document which is also _di_IDispatch)


Solution

  • I think I solved this myself. Instead of:

    DelphiInterface<IHTMLDocument2> diDoc =
    browser->Document;
    

    I simply used:

    #include <SHDocVw.hpp> // required for Shdocvw::IWebBrowser2
    #include "SHDocVw_OCX.h"    // TCppWebBrowser
    
    DelphiInterface<IHTMLDocument2> diDoc =
    DelphiInterface<Shdocvw::IWebBrowser2>(browser->DefaultDispatch)->Document;
    

    This also seems to work:

    DelphiInterface<IHTMLDocument2> diDoc =
    DelphiInterface<Shdocvw::IWebBrowser2>(browser->Application)->Document;
    

    Alternatively:

    DelphiInterface<IHTMLDocument2> diDoc = ((_di_IDispatch)browser->DefaultDispatch)->Document;
    DelphiInterface<IHTMLDocument2> diDoc = ((_di_IDispatch)browser->Application)->Document;
    

    The memory leak seems to be addressed with this just like in case of the Delphi equivalent linked in the answer above.