Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Default browser

How can I determine which is the default browser in my system programatically. The code must be developed using vc++ Is there any API for this ?

Where in the registry is the default browser value stored?

like image 283
subbu Avatar asked Apr 09 '09 07:04

subbu


4 Answers

You normally do not need to know this. ShellExecute(0,0,"http://stackoverflow.com",0,0,SW_SHOWNORMAL); will do the trick. Windows will spot the http:// and figure out from there that you want to open a URL. The "default" webbrowser is pretty much defined as the webbrowser used by Windows for this task.

It's not just http:// which is supported. ShellExecute can start the default webbrowser with https:// URLs as well. For mailto: URLs, it starts the default mail client.

like image 198
MSalters Avatar answered Oct 03 '22 09:10

MSalters


you can find the default browser in the registry

i.e. for Windows XP and Vista is located at

HKEY_LOCAL_MACHINE\Software\Clients\StartMenuInternet\
like image 42
Konstantinos Avatar answered Oct 03 '22 10:10

Konstantinos


TL;DR: If HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\ exists read that; otherwise read HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\.

After reading the answers here I found little concensus on how to detect the default browser so I did some experiments and research to figure it out. I downloaded the Firefox source, wrote a script that reads a bunch of registry entries and also ran Process Explorer all while changing the default browser over and over.

I found there are a lot of registry keys that Firefox and Chrome change when each sets itself as the default browser. I believe Safari and Opera are both similar in behavior. IE appears to change only one of the registry keys I was watching.

What I found was that while most browsers change other registry paths, all browsers change HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\ (default)

Here are the registry value from the registry key HKEY_CURRENT_USER\Software\Clients\StartMenuInternet\ (default) while each browser is the default browser.

  • IE 9.0.8112.16421: IEXPLORE.EXE
  • Chrome 21.0.1180.60 m: Google Chrome
  • Firefox 10.0.2: FIREFOX.EXE
  • Safari 3.2.2: Safari.exe
  • Opera 12.01: Opera

Tested on Microsoft Windows 7 Home Premium SP1 64-bit

Edit:

I found on a fresh install of Windows XP SP3 HKEY_CURRENT_USER\SOFTWARE\Clients\StartMenuInternet\ does not exist. In this case you should read the default browser from HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\. I suspect this is also the case on fresh installs of other versions of Windows.

Addendum:

The ShellExecute method is a great solution if all you want to do is open a web page in the default browser. However, if you want to, for example, install an extension in only the default browser, ShellExecute does not solve the problem.

like image 39
Nate Avatar answered Oct 03 '22 11:10

Nate


As its name suggests, StartMenuInternet is for registering a Web browser onto the Start Menu (and it only applies to XP and Vista, it is deprecated starting with Windows 7). That does not necessarily establish the browser as the default browser for the entire system. There are many different ways a browser can be registered for different purposes (loading a file, loading a URL, loading data based on a MIME type, etc). Each of those registrations are separate.

Default Programs

How to Register an Internet Browser or Email Client With the Windows Start Menu

Registering an Application to a URL Protocol

File Types

Personally, I would probably look at the registration of the "http" and/or "https" URL handler to determine the default browser, since that will be the app that loads when the user types a URL into the Start Menu or Windows Explorer, or an app passes a URL to ShellExecute/Ex().

like image 40
Remy Lebeau Avatar answered Oct 03 '22 09:10

Remy Lebeau