Search code examples
delphic++buildermshtmlihtmldocument2ihtmldocument

Assigning IHTMLDocument2 instance to a TWebBrowser instance


I am using an instance of the IHTMLDocument2 interface to parse some HTML as described in this post:

Load from IPersistMoniker takes long time to load unresolvable URL

The code is relatively simple:

DelphiInterface<IHTMLDocument2> diDoc2;
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (LPVOID*)&diDoc2);
// Load and process HTML here and save into memory stream or to disk after the processing is done

When I am done I save the HTML contents of the newly modified diDoc2 above and load the HTML into TWebBrowser.

  1. Can I instead just "assign" the already parsed IHTMLDocument2 above directly to the IHTMLDocument2 contained in the TWebBrowser, which seems would be much faster way of doing it. I can use probably IHTMLDocument2.write or some other method to do so, but there would be likely some performance penalty than simply assigning a pointer to already initialized object, if that is possible in the first place. In other words, I simply want to "show"/"render" what I have just parsed in the "back buffer" of sort.

  2. Is there a need to call CoInitialize and CoUninitialize before and after calling CoCreateInstance? I've seen some code which does that but it works without it, unless Delphi/C++ Builder do some under-the-hood initialization.


Solution

  • I used IHTMLDocument2.write and it appears to work well.

    WideString HTML = "<html><body>test</body></html>";
    if (diDoc)
        {
        // Creates a new one-dimensional array
        SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT,0,1);
    
        if (psaStrings)
            {
            VARIANT *param;
            BSTR bstr = SysAllocString(HTML.c_bstr());
            SafeArrayAccessData(psaStrings, (LPVOID*)&param);
            param->vt      = VT_BSTR;
            param->bstrVal = bstr;
            SafeArrayUnaccessData(psaStrings);
            diDoc->write(psaStrings);
            diDoc->close();
    
            // SafeArrayDestroy calls SysFreeString for each BSTR
            //SysFreeString(bstr);  // SafeArrayDestroy should be enough
            SafeArrayDestroy(psaStrings);
    
            return S_OK;
            }
        }
    
    return E_FAIL;