Detect Edge browser version for TTMSFNCWebBrowser

I want to integrate the TTMSFNCWebBrowser into my desktop applications, but I need a way to notify the user to install the Beta version of the Edge browser if its not installed on their computer.

I realize there is already a default message that appears saying "Could not initialize Edge Chromium! Please check Edge Chromium installation and verify correct version number."

But, that message will confuse my users. I want to make the experience more smooth by displaying instructions on how to install and where to download the edge browser and what version to install.

and I need to know how to detect if the beta or dev versions are installed on the user's computer before the app opens, and before the default warning message appears.

also, I want to avoid changing the source code of the FMX.TMSWebBrowser.Win where the default warning message is created.

I have a feeling the answer is probably simple, just ran out of ideas lol

anyhow help is much appreciated

Hi,


The loading of the browser DLLs happen in the initialization section, so the browser needs to be installed before the application is started. The detection is also version based. So no matter if you install Canary, Beta, DEV or stable, it automatically detects the correct version. Perhaps you could copy the initialization section code and use it in the form initialization section, but the webbrowser will need to be created programmatically.



unit Unit42;


interface


uses
  Windows, System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs;


type
  TForm42 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form42: TForm42;


implementation


{$R *.fmx}


const
  {$IFDEF WIN64}
  CoreWebView2DLL = 'WebView2Loader_x64.dll';
  {$ELSE}
  CoreWebView2DLL = 'WebView2Loader_x86.dll';
  {$ENDIF}


var
  GetAvailableCoreWebView2BrowserVersionString: function(browserExecutableFolder: PWideChar; versioninfo: PPWideChar): HRESULT; stdcall;


procedure TForm42.FormCreate(Sender: TObject);
var
  v: PWideChar;
  h: THandle;
begin
  h := LoadLibrary(CoreWebView2DLL);
  if (h = 0) then
  begin
    Log.d('WebView2 Loader DLLs not properly installed');
    Exit;
  end;


  try
    {$IFDEF LCLLIB}Pointer({$ENDIF}GetAvailableCoreWebView2BrowserVersionString{$IFDEF LCLLIB}){$ENDIF} := GetProcAddress(h, 'GetAvailableCoreWebView2BrowserVersionString');


    v := '';
    if Assigned(GetAvailableCoreWebView2BrowserVersionString) then
    begin
      GetAvailableCoreWebView2BrowserVersionString(nil, @v);
      //edge detected with version number:
      Log.d('edge chromium is found: ' + v);
    end
    else
      Log.d('edge chromium could not be found');
      //edge could not be found
  finally
    FreeLibrary(h);
  end;
end;


end.



awesome, thanks for the idea! I was able to figure out another way .. but it involved changing the source code of FMX.TMSWebBrowser.Win.


this is great, thanks for your help .. I really appreciate it

Great, If you want you can also share your idea

yes, absolutely ... here was my "hacked" idea:

STEP 1: Remove the warning messages from the FMX.TMSWebBrowser.Win file

in the following file: C:\Users\daveg\Documents\tmssoftware\TMS FNC Core\FMX.TMSWebBrowser.Win
in the following procedure TTMSFNCWinWebBrowser.Initialize

I simply commented out the

TTMSFNCUtils.Message(EErrorMessage)
TTMSFNCUtils.Message(EErrorMessageNoDLL)

parts so the default error messages do not appear when starting my app

STEP 2: I added a simple check when the user is going to use the feature of my app that requires a web browser:

{$IFDEF MSWINDOWS}
  var si:IInterface := FacebookLoginForm.WebBrowser2.GetWebBrowserInstance;
  if not Assigned(si) then
  begin
    //showmessage('no chrome');
    EdgeInfoForm.ShowModal; //this is a simple form with a label component explaining where to download the edge browser to the user
    exit;
  end;
{$ENDIF}


but, as I mentioned, I wanted to avoid changing the source code of the FMX.TMSWebBrowser.Win file. So your idea is much better for what I wanted. Here is how I implemented your idea so far (it might change):

STEP 1: put this at the beginning of the uses clause


{$IFDEF MSWINDOWS}
  Winapi.Windows,
{$ENDIF}


STEP 2: add this procedure in the class of my main form


{$IFDEF MSWINDOWS}
  procedure CheckEdgeBrowser;
{$ENDIF}


STEP 3: add this as a variable to the form


{$IFDEF MSWINDOWS}
  GetAvailableCoreWebView2BrowserVersionString: function(browserExecutableFolder: PWideChar; versioninfo: PPWideChar): HRESULT; stdcall;
{$ENDIF}


STEP 4: add the following procedure to the main form


{$IFDEF MSWINDOWS}
procedure TMainForm.CheckEdgeBrowser;
var
  v: PWideChar;
  h: THandle;
  edgeFound:boolean;
begin
  edgeFound := true;
  h := LoadLibrary(CoreWebView2DLL);
  if (h = 0) then
  begin
    showmessage('WebView2 Loader DLLs not properly installed');
    Application.Terminate;
    Halt; 
  end;


  try
    {$IFDEF LCLLIB}Pointer({$ENDIF}GetAvailableCoreWebView2BrowserVersionString{$IFDEF LCLLIB}){$ENDIF} := GetProcAddress(h, 'GetAvailableCoreWebView2BrowserVersionString');


    v := '';
    if Assigned(GetAvailableCoreWebView2BrowserVersionString) then
    begin
      GetAvailableCoreWebView2BrowserVersionString(nil, @v);
      if v = '' then edgeFound := false
      else edgeFound := true;
    end
    else edgeFound := false;


    //finally, show my custom instructions on how to install the edge chromimum browser
    if not edgeFound then
    begin
      Application.CreateForm(TEdgeInfoForm, EdgeInfoForm);
      EdgeInfoForm.ShowModal;
      Application.Terminate;
      Halt; //I know this is not good to do, but it seems to work for my particular app to stop further code being executed
    end;


  finally
    FreeLibrary(h);
  end;
end;
{$ENDIF}


STEP 5: call the procedure in the oncreate of the main form


{$IFDEF MSWINDOWS}CheckEdgeBrowser;{$ENDIF}


STEP 6: I create a simple form with a label component explaining users to install the edge browser with a link to the microsoft download page


Again, thanks for the help .. and I hope this code will assist someone else.

cheers

oops .. forgot to include the dll code in step 3




const
  {$IFDEF WIN64}
  CoreWebView2DLL = 'WebView2Loader_x64.dll';
  {$ELSE}
  CoreWebView2DLL = 'WebView2Loader_x86.dll';
  {$ENDIF}



Hi,



Thank you for the input, we might incorporate this as a class procedure that returns a type value or boolean, to know if the setup of the browser is correct or not. The code sample you posted is really helpful, thanks!!

I am running Windows Server 2008 R2, with Edge+Chromium installed. I used the 2nd set of steps. GetAvailableCoreWebView2BrowserVersionString(nil, @v); always sets v = nil. I have tried with Edge set as the default browser and Firefox set as the default browser. The outcome is the same. I also tried running on Windows 10 v2004 with Edge+Chromium installed. It behaves the same as WinSvr2008R2. If I ignore the check and just create the TTMSFMCWebBrowser at runtime, it will load web pages using Edge. So The TMS control finds Edge. However, I too need to verify Edge is installed before the end-user loads a web page.

Any suggestions on how to get GetAvailableCoreWebView2BrowserVersionString(nil, @v); to properly set v?

I see now, by tracing through VCL.TMSFNCWebBrowser.Win, that v = nil either means it is not installed or the stable version is installed. The next step is to query the registry HKCU\Software\Microsoft\Edge\BLBeacon and read the value version to get the installed version of Edge. It makes sense now what I need to do...

Hi James, My apologies for the late answer. Your second answer is correct. I hope Microsoft will add versioning or at least allow for proper detection of the stable version.