1

I have a software which requires the default browser installed on user computer.

Is there a way that I can get it?

Thanks

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Tuk Tuk
  • 57
  • 5

2 Answers2

1

An solution that correctly works on modern versions of Windows cannot be based on association with http protocol, as that's no longer reliable. It should rather be based on a solution like the answer by @GregT to How to determine the Windows default browser (at the top of the start menu).

So something like:

function GetBrowserCommand: string;
var
  UserChoiceKey: string;
  HtmlProgId: string;
begin
  UserChoiceKey :=
    'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice';
  if RegQueryStringValue(HKCU, UserChoiceKey, 'ProgId', HtmlProgId) then
  begin
    Log(Format('ProgID to registered for .html is [%s].', [HtmlProgId]));
    if RegQueryStringValue(HKCR, HtmlProgId + '\shell\open\command', '', Result) then
    begin
      Log(Format('Command for ProgID [%s] is [%s].', [HtmlProgId, Result]));
    end;
  end;

  { Fallback for old version of Windows }
  if Result = '' then
  begin
    if RegQueryStringValue(HKCR, 'http\shell\open\command', '', Result) then
    begin
      Log(Format('Command registered for http: [%s].', [Result]));
    end;
  end;
end;

If you want to extract browser path from the command, use a code like:

function ExtractProgramPath(Command: string): string;
var
  P: Integer;
begin
  if Copy(Command, 1, 1) = '"' then
  begin
    Delete(Command, 1, 1);
    P := Pos('"', Command);
  end
    else P := 0;

  if P = 0 then
  begin
    P := Pos(' ', Command);
  end;
  Result := Copy(Command, 1, P - 1);
end;

(based on Executing UninstallString in Inno Setup)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Take this:

function GetBrowser() : String;
var
  RegistryEntry: String;
  Browser: String;
  Limit: Integer   ;
begin
  if RegQueryStringValue(HKEY_CLASSES_ROOT, 'http\shell\open\command', '', RegistryEntry) then
  begin
    Limit := Pos('.exe' ,RegistryEntry)+ Length('.exe');
    Browser := Copy(RegistryEntry, 1, Limit  );
    MsgBox('Your browser: ' + Browser , mbInformation, MB_OK);
  end;
end;
CAD Developer
  • 1,532
  • 2
  • 21
  • 27
  • This is does not work in modern versions of Windows anymore. For example I have `firefox.exe` there, while my default browser is Chrome. – Martin Prikryl May 18 '20 at 05:55
  • https://stackoverflow.com/questions/2177957/how-to-determine-the-windows-default-browser-at-the-top-of-the-start-menu someone closed my question saying this is the answer but i was expecting for a windows api to detect that. – Tuk Tuk May 18 '20 at 07:06
  • I understand that you wanted a code (though this does not use Wiodows API). But again, this is not valid answer anymore. The correct answer must be based on the answer by @GregT at https://stackoverflow.com/a/12444963/850848. – Martin Prikryl May 18 '20 at 08:30