[Windows] GetDefaultBrowser()

Share your advanced PureBasic knowledge/code with the community.
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

[Windows] GetDefaultBrowser()

Post by PureLust »

Relating to THIS Thread in the german forum, I've written a small Routine to get the default Browser.
Because it considers the changes in setting the default Browser since Windows-Vista, it should work for any Windows Version since Win-98.

Maybe this could be helpful to someone:

Code: Select all

Procedure.s ReadRegKey(OpenKey.l,SubKey.s,ValueName.s)
  hKey.l=0
  keyvalue.s=Space(255)
  datasize.l=255
 
  If RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
    keyvalue="Error Opening Key"
  Else
    If RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize)
      keyvalue="Error Reading key"
    Else 
      keyvalue=Left(keyvalue,datasize-1)
    EndIf
    RegCloseKey_(hKey)
  EndIf
 
  ProcedureReturn keyvalue
EndProcedure 

Procedure.s GetDefaultBrowser()
  Protected HTMLClass.s, HTMLCommand.s
  If OSVersion() = #PB_OS_Windows_Vista Or OSVersion() = #PB_OS_Windows_7 Or OSVersion()=#PB_OS_Windows_Future
    HTMLClass   = ReadRegKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice","ProgID")
    HTMLCommand = ReadRegKey(#HKEY_LOCAL_MACHINE, "SOFTWARE\Classes\"+HTMLClass+"\shell\open\command", "")
  Else
    HTMLClass   = ReadRegKey(#HKEY_CLASSES_ROOT, ".html", "")
    HTMLCommand = ReadRegKey(#HKEY_CLASSES_ROOT, HTMLClass+"\shell\open\command", "")
  EndIf
  ProcedureReturn GetPathPart(HTMLCommand) + GetFilePart(HTMLCommand)
EndProcedure

Procedure.s GetDefaultBrowser2()
  ; This Procedure does NOT need the Procedure: ReadRegKey()
  Protected hKey.l      = 0
  Protected keyvalue.s  = Space(255)
  Protected datasize.l  = 255

  Protected OpenKey.l   = #HKEY_CURRENT_USER
  Protected SubKey.s    = "SOFTWARE\Clients\StartMenuInternet"
  Protected ValueName.s = ""
  Protected Browser.s   = ""

  ; Check if DefaultBrowser-Setting exists for Local-User
  If Not RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
    If Not RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize)
      Browser = Left(keyvalue,datasize-1)
    EndIf
    RegCloseKey_(hKey)
  EndIf
  ; If Local-User has not special DefaultBrowser-Setting, read Browser-Setting from Local-Machine
  If Browser = ""
    OpenKey.l   = #HKEY_LOCAL_MACHINE
    If Not RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
      If Not RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize)
        Browser = Left(keyvalue,datasize-1)
      EndIf
      RegCloseKey_(hKey)
    EndIf
  EndIf
  ; Get full Path-Information for DefaultBrowser
  If Browser <> ""
    datasize.l  = 255
    OpenKey.l   = #HKEY_LOCAL_MACHINE
    SubKey.s    = "SOFTWARE\Clients\StartMenuInternet\"+Browser+"\shell\open\command"
    If Not RegOpenKeyEx_(OpenKey,SubKey,0,#KEY_READ,@hKey)
      If Not RegQueryValueEx_(hKey,ValueName,0,0,@keyvalue,@datasize)
        Browser = Left(keyvalue,datasize-1)
      EndIf
      RegCloseKey_(hKey)
    EndIf
  EndIf
  ProcedureReturn Trim(Browser,Chr(34))
EndProcedure

Debug GetDefaultBrowser()
Debug GetDefaultBrowser2()
[Edit 1:]
Because I found a lot of inconsistent informations how to get the Default-Browser, I've added another procedure which reads the Default-Browser in another way.

It would be nice if some of you could try both procedures and could reply which one works better for you.
(Informations about your OS-Version, your Browser, and which Browser will be opened on your system if you start a HTML-File or if a Program calls an URL would be helpful.)

Thanks, PureLust.
(Search Topics: StandardBrowser StandartBrowser DefaultBrowser Standard Standart Default Browser )
Last edited by PureLust on Sat Nov 28, 2009 4:49 am, edited 4 times in total.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [Windows] GetDefaultBrowser()

Post by luis »

Hi, just FYI on my system (XP SP3 ITA) says IE but my default browser is FireFox 3.

BTW, .htm and .html files are opened by IE, when double clicked.

IE when checking, says it's not the predefined browser.

FireFox when checking, says it's the predefined browser.

Other programs, when launching the "default" browser to open a url for example, are launching FireFox.


EDIT:
with the new version (the one with two procs)

the first one says IE as above
the second one says (correctly) Firefox
Last edited by luis on Sat Nov 28, 2009 6:30 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: [Windows] GetDefaultBrowser()

Post by eJan »

When registering Internet Browser: HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet
(Default) REG_SZ value holds Browser Info.
Image
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: [Windows] GetDefaultBrowser()

Post by PureLust »

Because I found a lot of inconsistent informations how to get the REAL Default-Browser, I've added another procedure which reads the Default-Browser in another way.

It would be nice if some of you could try both procedures and could reply which one works better for you.
(Informations about your OS-Version, your Browser, and which Browser will be opened on your system if you start a HTML-File or if a Program calls an URL would be helpful.)

Thanks, PL.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: [Windows] GetDefaultBrowser()

Post by rsts »

PureLust wrote:Because I found a lot of inconsistent informations how to get the REAL Default-Browser, I've added another procedure which reads the Default-Browser in another way.

It would be nice if some of you could try both procedures and could reply which one works better for you.
(Informations about your OS-Version, your Browser, and which Browser will be opened on your system if you start a HTML-File or if a Program calls an URL would be helpful.)

Thanks, PL.
Error Opening Key
P:\Program Files (x86)\Internet Explorer\iexplore.exe

The second being correct. Windows 7 x64 with 32 bit ie8 as default browser.

cheers
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: [Windows] GetDefaultBrowser()

Post by PureLust »

Hi rsts - thanks for testing.
rsts wrote:Error Opening Key
So the checked Reg-Key doesn't exist under Win-7 anymore.
That's a bit funny, because (relating to the Infos I found) M$ has just established this key with Win-Vista. :roll:

Would be interesting how both Routines will prove on older OSes.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: [Windows] GetDefaultBrowser()

Post by ts-soft »

Both routines have the same result on my win7 x64: "Firefox Only"
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: [Windows] GetDefaultBrowser()

Post by UserOfPure »

luis wrote:BTW, .htm and .html files are opened by IE, when double clicked.
Only if the user hasn't changed the associations... one has to take that into account, as people do change it.
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: [Windows] GetDefaultBrowser()

Post by eJan »

Both works fine:
"C:\Program Files\Maxthon2\Maxthon.exe
C:\Program Files\Maxthon2\Maxthon.exe"
Image
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: [Windows] GetDefaultBrowser()

Post by luis »

Retried and edited my first post.
PureLust wrote: (Informations about your OS-Version, your Browser, and which Browser will be opened on your system if you start a HTML-File or if a Program calls an URL would be helpful.)
Already done ! (class isn't water) :lol:
"Have you tried turning it off and on again ?"
Post Reply