Code archives/Miscellaneous/Get default browser path (Windows only)

This code has been declared by its author to be Public Domain code.

Download source code

Get default browser path (Windows only) by BlitzSupport2013
Retrieves the path to the Windows default browser; can also check for, and retrieve paths to, a few common browsers by passing specific strings.

Could feasibly be used to determine the 'best' browser available for a specific use, eg. viewing a set of help docs; for instance, I wrote a program intended to open a bunch of tabs in one go, which, weirdly, Internet Explorer still can't do from the command line (no, really), even at version 10! If IE was the default browser, I looked for others, and failing that just gave an error message. (If others were available, I used one of those instead.)

Not heavily tested, but seems to work on both Windows 7 64-bit and a Windows 8 system I don't have direct access to... I would expect it to work on at least 32-bit XP and above.
' Some registry references to common browsers:

' Note that, for most web browsers, this string can be found in the registry under:

' HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\WEB_BROWSER\Capabilities\FileAssociations\.html

' ... hence it should be fairly easy to check for other browsers by passing this string to GetBrowserPath...

Const IE:String			= "IE.HTTP"
Const FIREFOX:String	= "FirefoxHTML"
Const CHROME:String		= "ChromeHTML"
Const OPERA:String		= "Opera.HTML"
Const OPERANEXT:String	= "Operanext"

Extern "win32"

	Const ERROR_SUCCESS:Int			= 0

	Const HKEY_CLASSES_ROOT:Int		= $80000000
	Const HKEY_CURRENT_USER:Int		= $80000001
	Const HKEY_LOCAL_MACHINE:Int	= $80000002
	Const HKEY_USERS:Int			= $80000003
	Const HKEY_CURRENT_CONFIG:Int	= $80000005
	Const KEY_READ:Int				= $20019

	Function RegOpenKeyExA (hKey:Int, lpSubKey:Byte Ptr, ulOptions:Int, samDesired:Int, phkResult:Byte Ptr)
	Function RegQueryValueExA (hKey:Int, lpValueName:Byte Ptr, lpReserved:Byte Ptr, lpType:Byte Ptr, lpData:Byte Ptr, lpcbData:Byte Ptr)
	Function RegCloseKey (hKey:Int)

End Extern

Function GetBrowserPath:String (force:String = "")

	' Can pass "default" or "" for default browser...
	
	If Lower (force) = "default"
		force = ""
	EndIf
	
	Local browser:Byte []	' C string to receive browser path
	Local key:Int			' Registry key handle (re-used repeatedly)
	
	' Find user choice browser ID...
	
	If RegOpenKeyExA (HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice".ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
	
		If key
		
			Local size:Int
			
			If RegQueryValueExA (key, "Progid".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
				Local classname:Byte [size]
				RegQueryValueExA (key, "Progid".ToCString (), Null, Null, classname, Varptr size)
			EndIf
		
			RegCloseKey key
		
			Local browserclassname:String = String.FromCString (classname)
			
			' Got it, but caller wants to override to retrieve specific browser path...
			
			If force
				browserclassname = force
			EndIf
			
			' Use browser ID to get path...
			
			If RegOpenKeyExA (HKEY_CLASSES_ROOT, (String.FromCString (browserclassname) + "\shell\open\command").ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
				If RegQueryValueExA (key, "".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
					browser = New Byte [size]
					RegQueryValueExA (key, "".ToCString (), Null, Null, browser, Varptr size)
				EndIf
				RegCloseKey key
			EndIf
			
			Local defaultbrowser:String = String.FromCString (browser)
			Local quote2:Int	' Used to find second quote in browser string...
			
			' Usually "path/subpath/browser.exe" plus some extra crap. Just want browser in quotes...
			
			If defaultbrowser
				quote2 = Instr (defaultbrowser, Chr (34), 2) - 1
				defaultbrowser = Chr (34) + Mid (defaultbrowser, 2, quote2 - 1) + Chr (34)
			Else
			
				' Still nothing? Try generic http handler instead...
				
				If RegOpenKeyExA (HKEY_CLASSES_ROOT, "http\shell\open\command".ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
					If RegQueryValueExA (key, "".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
						browser = New Byte [size]
						RegQueryValueExA (key, "".ToCString (), Null, Null, browser, Varptr size)
					EndIf
					RegCloseKey key
				EndIf

				defaultbrowser = String.FromCString (browser)

				' Strip junk...
				
				If defaultbrowser
					quote2 = Instr (defaultbrowser, Chr (34), 2) - 1
					defaultbrowser = Chr (34) + Mid (defaultbrowser, 2, quote2 - 1) + Chr (34)
				EndIf

			EndIf
			
			Return defaultbrowser
			
		EndIf
	
	EndIf

End Function

' D E M O . . .

Print ""
Print "Checking for default browser..."
Print ""

	Print "~tDefault: " + GetBrowserPath () ' or "Default" or ""

Print ""
Print "Checking for a few common browsers..."
Print ""

	Print "~tInternet Explorer: "	+ GetBrowserPath (IE)
	Print "~tFirefox: "		+ GetBrowserPath (FIREFOX)
	Print "~tChrome: "		+ GetBrowserPath (CHROME)
	Print "~tOpera: "		+ GetBrowserPath (OPERA)
	Print "~tOpera Next: "		+ GetBrowserPath (OPERANEXT)

' Done!

Comments

_PJ_2013
That first registry key only exists if users have installed, run and selected the "Browser Choice Menu".
The http (htt) root class Key also may not exist, or may not even be set to open with a browser, if, for example, someone has set their Open command to run a web-page design program etc.

Since this is for Windows only, and I doubt anybody, especially if viewing this on the website noawadays, is using Netscape Navigator, so I think it may be safe to set a default of IE for when these Registry Keys fail.
It's also possible for Registry to be locked (via GP or individual permission sets) against applications (or apps run by particular users) to fail.
All such failures will return ""


xlsior2013
That first registry key only exists if users have installed, run and selected the "Browser Choice Menu".


Which, FWIW, only exists in the European releases of Windows. The majority of windows users don't get that option.


BlitzSupport2013

[Browser Choice Menu] only exists in the European releases of Windows. The majority of windows users don't get that option.



Ah, you learn something every day! But still, as a fallback...


The http (htt) root class Key also may not exist, or may not even be set to open with a browser, if, for example, someone has set their Open command to run a web-page design program etc.



There must be very few systems where this doesn't exist (I mean, what happens if you click on a web shortcut with no browser specified in either of the above locations?).

In cases where it's been set to another type of application, that application must surely 'become' the default web browser?

Defaulting to IE isn't necessarily safe, either, of course, as it can be uninstalled by 3rd party programs (or not even installed in the first place depending on the setup in bespoke Windows installers).

There's nothing to stop a real-world application asking before opening the default browser (and perhaps saving the answer), or providing an option to disable opening, or offering to run a browser from a given path.

I think the above makes for a reasonably wide net, but I of course realise that you can't take anything for granted.


Code Archives Forum