Sometimes we want to know what web browsers is installed in the computer where our program is running. This tutorial will show you how to detect installed web browsers using Delphi codes. Codes used in this tutorial and the demo project was written with Delphi 7. But I believe they were valid for other Delphi versions and FreePascal/Lazarus.
As usual, we should inspect windows registry to see if information of installed web browsers is available there. Fortunately it is. After researching the registry I found that general information of installed web browsers is stored under this key: HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet.
Each installed web browser will have at least a subkey under HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet (from now on we will refer it as Web Browsers Root Key) that hold general information about itself. Unfortunately a web browser might created additional subkeys to store additional information. Lucky for us, we can easily detect which one is the "official" one since it always has the web browser name for the default value of the subkey.
For example, my registry for the Web Browsers Root Key looks like below:
Reading Web Browsers Information
To properly use OOP (Object Oriented Programming), I created two following classes, and store them in a new unit I named WebBrowsers.pas.
- TWebBrowser to represent a web browser installed in the system.
- TWebBrowsers to store multiple instances of TWebBrowser and also to provide a way to read installed web browsers.
In the current case, this class has only little use except to convey the names of the installed web browsers. However later we can extend it to provide more functionalities.
The main function of this class is to hold instances of TWebBrowser objects for easy manipulation. It has interesting method named ReadFromSystem which will read installed web browser information, create an instance of TWebBrowser for each detected web browser, and store the created instances of TWebBrowser in itself.
Declaration of TWebBrowsers:
type TWebBrowsers=class private FItems: TObjectList; function GetIsOwnItems: Boolean; function GetCount: Integer; function GetItem(Index: Integer): TWebBrowser; public constructor Create(const AOwnItems: Boolean); destructor Destroy; override; // Clear the content of this collection. When its IsOwnItems is true, // the content will also be destroyed upon clearing procedure Clear; // Clear and then populate this collection with TWebBrowser instance(s) // corresponding with installed web browser(s) in the system procedure ReadFromSystem; // Flag that indicates if this collection is responsible to destroy its content property IsOwnItems: Boolean read GetIsOwnItems; property Count: Integer read GetCount; property Items[Index: Integer]: TWebBrowser read GetItem; default; end;
And implementation of TWebBrowser.ReadFromSystem:
procedure TWebBrowsers.ReadFromSystem; const cBrowsersRootKey = 'SOFTWARE\Clients\StartMenuInternet'; var i: Integer; vReg: TRegistry; vName: string; vBrowserKeys: TStrings; vWebBrowser: TWebBrowser; begin // Empty the current content, if any Clear; vReg := TRegistry.Create; try // we deal only with HKEY_LOCAL_MACHINE root vReg.RootKey := HKEY_LOCAL_MACHINE; vBrowserKeys := TStringList.Create; try // Open the "Web Browsers Root Key" if not vReg.OpenKeyReadOnly(cBrowsersRootKey) then Exit; // get the subkeys names vReg.GetKeyNames(vBrowserKeys); // Just get out of here immediately if there is no subkey found if vBrowserKeys.Count=0 then Exit; for i := 0 to vBrowserKeys.Count-1 do begin vReg.CloseKey; // try to open a subkey to a possible webbrowser general info if not vReg.OpenKeyReadOnly(cBrowsersRootKey + '\' + vBrowserKeys) then Continue; // get the "offical" name of the web browser from default value of the subkey vName := vReg.ReadString(''); vReg.CloseKey; // if no name specified, we are not in web browser "main" subkey if vName='' then Continue; // Store the general information into an instance of TWebBrowser and // store it in the internal collection vWebBrowser := TWebBrowser.Create; vWebBrowser.FName := vName; FItems.Add(vWebBrowser); end; finally vBrowserKeys.Free; end; finally vReg.Free; end; end;
- Create a new application. Keep the default name Project1 and Form1 and store them.
- Drop a TMemo to Form1, adjust its size and position so to cover nearly all client are of Form1. Leave its name to the default of Memo1
- Drop a TButton just below Memo1. Adjust its Left and Width properties to match Memo1's. Leave its name to the default of Button1.
Coding The GUI
- Add WebBrowsers.pas unit to our demo project. Use menu Project-Add to Project.
- Open Form1 in form design mode.
- Make sure Form1 uses WebBrowsers.pas. Use menu File-Use unit... for this.
- Double click on Button1. This will generate skeleton code for Button1's OnClick event handler.
- Use the following codes for Button1's OnClick event handler.
procedure TForm1.Button1Click(Sender: TObject); var i: Integer; vWebBrowsers: TWebBrowsers; begin Memo1.Clear; vWebBrowsers := TWebBrowsers.Create(True); try vWebBrowsers.ReadFromSystem; if vWebBrowsers.Count=0 then Exit; for i := 0 to vWebBrowsers.Count-1 do begin Memo1.Lines.Add(vWebBrowsers[i].Name); Memo1.Lines.Add(''); end; finally vWebBrowsers.Free; end; end;
Press F9 to compile our project and run it. Initially you will get empty form like this.
Click on "Get Installed Web Browsers Information" button and the memo control will be filled with installed web browsers information. Just like shown below.
From the above picture, it is clear that in the test computer I have 3 web browsers installed, i.e.:
- Google Chrome
- Internet Explorer
Demo.zip 202.12KB 568 downloads
Edited by LuthfiHakim, 10 February 2013 - 09:41 AM.