December 04, 2020, 11:14:45 AM

Author Topic: [bmx] Get default browser path (Windows only) by BlitzSupport [ 1+ years ago ]  (Read 380 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Get default browser path (Windows only)
Author : BlitzSupport
Posted : 1+ years ago

Description : 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.


Code :
Code: BlitzMax
  1. ' Some registry references to common browsers:
  2.  
  3. ' Note that, for most web browsers, this string can be found in the registry under:
  4.  
  5. ' HKEY_LOCAL_MACHINESOFTWAREClientsStartMenuInternetWEB_BROWSERCapabilitiesFileAssociations.html
  6.  
  7. ' ... hence it should be fairly easy to check for other browsers by passing this string to GetBrowserPath...
  8.  
  9. Const IE:String                 = "IE.HTTP"
  10. Const FIREFOX:String    = "FirefoxHTML"
  11. Const CHROME:String             = "ChromeHTML"
  12. Const OPERA:String              = "Opera.HTML"
  13. Const OPERANEXT:String  = "Operanext"
  14.  
  15. Extern "win32"
  16.  
  17.         Const ERROR_SUCCESS:Int                 = 0
  18.  
  19.         Const HKEY_CLASSES_ROOT:Int             = $80000000
  20.         Const HKEY_CURRENT_USER:Int             = $80000001
  21.         Const HKEY_LOCAL_MACHINE:Int    = $80000002
  22.         Const HKEY_USERS:Int                    = $80000003
  23.         Const HKEY_CURRENT_CONFIG:Int   = $80000005
  24.         Const KEY_READ:Int                              = $20019
  25.  
  26.         Function RegOpenKeyExA (hKey:Int, lpSubKey:Byte Ptr, ulOptions:Int, samDesired:Int, phkResult:Byte Ptr)
  27.         Function RegQueryValueExA (hKey:Int, lpValueName:Byte Ptr, lpReserved:Byte Ptr, lpType:Byte Ptr, lpData:Byte Ptr, lpcbData:Byte Ptr)
  28.         Function RegCloseKey (hKey:Int)
  29.  
  30. End Extern
  31.  
  32. Function GetBrowserPath:String (force:String = "")
  33.  
  34.         ' Can pass "default" or "" for default browser...
  35.        
  36.         If Lower (force) = "default"
  37.                 force = ""
  38.         EndIf
  39.        
  40.         Local browser:Byte []   ' C string to receive browser path
  41.         Local key:Int                   ' Registry key handle (re-used repeatedly)
  42.        
  43.         ' Find user choice browser ID...
  44.        
  45.         If RegOpenKeyExA (HKEY_CURRENT_USER, "SoftwareMicrosoftWindowsCurrentVersionExplorerFileExts.htmlUserChoice".ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
  46.        
  47.                 If key
  48.                
  49.                         Local size:Int
  50.                        
  51.                         If RegQueryValueExA (key, "Progid".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
  52.                                 Local classname:Byte [size]
  53.                                 RegQueryValueExA (key, "Progid".ToCString (), Null, Null, classname, Varptr size)
  54.                         EndIf
  55.                
  56.                         RegCloseKey key
  57.                
  58.                         Local browserclassname:String = String.FromCString (classname)
  59.                        
  60.                         ' Got it, but caller wants to override to retrieve specific browser path...
  61.                        
  62.                         If force
  63.                                 browserclassname = force
  64.                         EndIf
  65.                        
  66.                         ' Use browser ID to get path...
  67.                        
  68.                         If RegOpenKeyExA (HKEY_CLASSES_ROOT, (String.FromCString (browserclassname) + "shellopencommand").ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
  69.                                 If RegQueryValueExA (key, "".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
  70.                                         browser = New Byte [size]
  71.                                         RegQueryValueExA (key, "".ToCString (), Null, Null, browser, Varptr size)
  72.                                 EndIf
  73.                                 RegCloseKey key
  74.                         EndIf
  75.                        
  76.                         Local defaultbrowser:String = String.FromCString (browser)
  77.                         Local quote2:Int        ' Used to find second quote in browser string...
  78.                        
  79.                         ' Usually "path/subpath/browser.exe" plus some extra crap. Just want browser in quotes...
  80.                        
  81.                         If defaultbrowser
  82.                                 quote2 = Instr (defaultbrowser, Chr (34), 2) - 1
  83.                                 defaultbrowser = Chr (34) + Mid (defaultbrowser, 2, quote2 - 1) + Chr (34)
  84.                         Else
  85.                        
  86.                                 ' Still nothing? Try generic http handler instead...
  87.                                
  88.                                 If RegOpenKeyExA (HKEY_CLASSES_ROOT, "httpshellopencommand".ToCString (), 0, KEY_READ, Varptr key) = ERROR_SUCCESS
  89.                                         If RegQueryValueExA (key, "".ToCString (), Null, Null, Null, Varptr size) = ERROR_SUCCESS
  90.                                                 browser = New Byte [size]
  91.                                                 RegQueryValueExA (key, "".ToCString (), Null, Null, browser, Varptr size)
  92.                                         EndIf
  93.                                         RegCloseKey key
  94.                                 EndIf
  95.  
  96.                                 defaultbrowser = String.FromCString (browser)
  97.  
  98.                                 ' Strip junk...
  99.                                
  100.                                 If defaultbrowser
  101.                                         quote2 = Instr (defaultbrowser, Chr (34), 2) - 1
  102.                                         defaultbrowser = Chr (34) + Mid (defaultbrowser, 2, quote2 - 1) + Chr (34)
  103.                                 EndIf
  104.  
  105.                         EndIf
  106.                        
  107.                         Return defaultbrowser
  108.                        
  109.                 EndIf
  110.        
  111.         EndIf
  112.  
  113. End Function
  114.  
  115. ' D E M O . . .
  116.  
  117. Print ""
  118. Print "Checking for default browser..."
  119. Print ""
  120.  
  121.         Print "~tDefault: " + GetBrowserPath () ' or "Default" or ""
  122.  
  123. Print ""
  124. Print "Checking for a few common browsers..."
  125. Print ""
  126.  
  127.         Print "~tInternet Explorer: "   + GetBrowserPath (IE)
  128.         Print "~tFirefox: "             + GetBrowserPath (FIREFOX)
  129.         Print "~tChrome: "              + GetBrowserPath (CHROME)
  130.         Print "~tOpera: "               + GetBrowserPath (OPERA)
  131.         Print "~tOpera Next: "          + GetBrowserPath (OPERANEXT)
  132.  
  133. ' Done!


Comments :


_PJ_(Posted 1+ years ago)

 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 ""


xlsior(Posted 1+ years ago)

 <div class="quote"> That first registry key only exists if users have installed, run and selected the "Browser Choice Menu". </div>Which, FWIW, only exists in the European releases of Windows. The majority of windows users don't get that option.


BlitzSupport(Posted 1+ years ago)

 <div class="quote"> [Browser Choice Menu] only exists in the European releases of Windows. The majority of windows users don't get that option. </div>Ah, you learn something every day! But still, as a fallback...<div class="quote"> 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. </div>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.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal