Getting Default browser

2019-01-26 09:47发布

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?

5条回答
手持菜刀,她持情操
2楼-- · 2019-01-26 10:16

Read the default value of HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet and optionally check HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\XXX\shell\open\command where XXX is that value picked up from the first key.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-26 10:27

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.

查看更多
做自己的国王
4楼-- · 2019-01-26 10:30

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().

查看更多
贼婆χ
5楼-- · 2019-01-26 10:31

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\
查看更多
霸刀☆藐视天下
6楼-- · 2019-01-26 10:33

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.

查看更多
登录 后发表回答