Search code examples
microsoft-edgec++buildertedgebrowser

TEdgeBrowser and CoreWebView2 printing in C++ Builder


CoreWebView2 exposes methods like ShowPrintUI which I'd like to utilize.

However, it seems that RAD Studio, even in the latest version 11.3 has hpp files from a pretty old build - 1.0.1054.31 from 2021, at least that is shown in the GetIt Package Manager. This has not been updated for a very long time and Embarcadero seems to be very slow in updating this.

It appears that the ShowPrintUI requires at least 1.0.1518.46 version of the SDK.

Is there a way to add those headers to the current C++ Builder installation, or at least the relevant part for using CoreWebView2.ShowPrintUI() or CoreWebView2.PrintAsync() and how specifically?

I already know about the workaround to execute JavaScript like window.print() which I do not want to use as a solution as it doesn't pop up a new window but shows the user interface inside of the TEdgeBrowser window.

I am guessing that, if newer headers would be available the usage would be as easy as:

EdgeBrowser1->DefaultInterface->ShowPrintUI();

Per support reply by Embarcadero representative, they plan to update headers in the future major release, but I'd like to avoid the wait as I also have to use this in an older version of C++ Builder.


Solution

  • I have solved the problem by updating the WebView2.pas and WebView2.hpp, based on this answer:

    WebView2 (TEdgeBrowser) updated Delphi interface (e.g. ICoreWebView2Controller2)

    One obstacle I encountered was that some interfaces which were generated by tlibimp.exe were duplicates, already defined in other include files.

    Some duplicates were:

    IDataObject = interface;
    IEnumFORMATETC = interface;
    IAdviseSink = interface;
    IPersist = interface;
    IPersistStream = interface;
    IMoniker = interface;
    IBindCtx = interface;
    IRunningObjectTable = interface;
    IEnumMoniker = interface;
    IEnumString = interface;
    IEnumSTATDATA = interface;
    

    Also, some other symbols needed things like {$EXTERNALSYM GUID} or {$EXTERNALSYM tagRECT} and a few others.

    Additionally, some variable names were conflicting with C++ such as:

    function AddHostObjectToScript(name: PWideChar; const object: OleVariant): HResult; stdcall;
    

    Which I had to redefine to:

    function AddHostObjectToScript(name: PWideChar; const object_: OleVariant): HResult; stdcall;
    

    Most of these changes I was able to figure out how to properly remove or redefine by comparing the generated WebView2.pas and WebView2.hpp to the original one included with RAD Studio including some of the above redefinitions and external symbols.

    After a few hours of doing this, I ended up with working new interfaces and now I can utilize all the recent interfaces released in the meantime since Embarcadero last update (which was beta version of Edge Chromium interface) and the most recent one.

    It would be really nice if Embarcadero would include updated interface, which I have also requested in their QC portal.