Page 1 of 1

[Windows] GetDefaultBrowser()

Posted: Fri Nov 27, 2009 11:25 am
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 )

Re: [Windows] GetDefaultBrowser()

Posted: Fri Nov 27, 2009 11:29 pm
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

Re: [Windows] GetDefaultBrowser()

Posted: Fri Nov 27, 2009 11:55 pm
by eJan
When registering Internet Browser: HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet
(Default) REG_SZ value holds Browser Info.

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 4:46 am
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.

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 6:00 am
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

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 11:55 am
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.

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 11:59 am
by ts-soft
Both routines have the same result on my win7 x64: "Firefox Only"

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 3:12 pm
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.

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 4:02 pm
by eJan
Both works fine:
"C:\Program Files\Maxthon2\Maxthon.exe
C:\Program Files\Maxthon2\Maxthon.exe"

Re: [Windows] GetDefaultBrowser()

Posted: Sat Nov 28, 2009 6:33 pm
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: