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!!