Get Windows Internet Default Browser

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Get Windows Internet Default Browser

Post by RASHAD »

Windows XP,Vista,7 x86 - x64

Code: Select all


Procedure.s GetTempDirectory()
  Protected WinTemp.s 
  WinTemp  = Space(255) 
  GetTempPath_(255, WinTemp) 
  If Right(WinTemp, 1) <> "\" : WinTemp = WinTemp + "\" : EndIf 
  ProcedureReturn WinTemp 
EndProcedure

Associatefile$ = Space(1024)

CreateFile(0,GetTempDirectory()+"test.html")
WriteString(0,"<HTML> <\HTML>")
CloseFile(0)

FindExecutable_("test.html",GetTempDirectory(),@Associatefile$)
MessageRequester("Default Internet Browser",GetFilePart(Associatefile$))
DeleteFile(GetTempDirectory()+"test.html")
Have fun
Egypt my love
UserOfPure
Enthusiast
Enthusiast
Posts: 469
Joined: Sun Mar 16, 2008 9:18 am

Re: Get Windows Internet Default Browser

Post by UserOfPure »

(1) You can replace your entire GetTempDirectory procedure with the GetTemporaryDirectory() command.

(2) Your code just returns the app used to open HTML files -- it doesn't get the default browser as such (even though they're probably the same 99% of the time). For example, my default browser is Firefox, but my PC opens HTML files with Iexplore. So, running your code shows Iexplore as my default browser when it's not.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser

Post by RASHAD »

UserOfPure
Your first comment is much appreciated and I will use it
Your second about the Other 1% I will try to investigate it first
Then may be I will find a way to accommodate it
Thank you very much

Code: Select all


Associatefile$ = Space(1024)

CreateFile(0,GetTemporaryDirectory() +"test.html")
WriteString(0,"<HTML> <\HTML>")
CloseFile(0)

FindExecutable_("test.html",GetTemporaryDirectory(),@Associatefile$)
MessageRequester("Default Internet Browser",GetFilePart(Associatefile$))
DeleteFile(GetTemporaryDirectory()+"test.html")
Egypt my love
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Get Windows Internet Default Browser

Post by Little John »

Yesterday there already has been created another thread that shows how to get the default internet browser on windows.

Regards, Little John
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser

Post by RASHAD »

Sorry Little John
This is Tricks 'n' Tips Not Coding Ques.
If you have a tip of your own you must post it in a new thread
But if you have a comment on any one tip or a little modification you have to use post reply
Egypt my love
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Get Windows Internet Default Browser

Post by ts-soft »

RASHAD wrote:Sorry Little John
This is Tricks 'n' Tips Not Coding Ques.
If you have a tip of your own you must post it in a new thread
But if you have a comment on any one tip or a little modification you have to use post reply
The result of the other thread is what your title suggest.
Your result is the file to open html, but not the Internet Default Browser.

Greetings
Thomas
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Get Windows Internet Default Browser

Post by Little John »

RASHAD wrote:Sorry Little John
This is Tricks 'n' Tips Not Coding Ques.
If you have a tip of your own you must post it in a new thread
But if you have a comment on any one tip or a little modification you have to use post reply
:?: :?: :?:

I just wanted to provide a link to the other thread with (almost) the same topic, so that later for people who are searching code it's easier to find what they are looking for.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser[Updated]

Post by RASHAD »

I do't know when anyone will need to check for the Default Web Browser
But As UserOfPure pointed to in his post
This piece of code checks for the Real Default URL Browser
Tested on XP,Vista,Windows 7 x86 x64 For IE & FireFox Only

Code: Select all


lRetVal.i
sRemMachName.s
lTopLevelKey.i
lHKeyhandle.i
sKeyName.s
lhkey.i
sValueName.s
vValue.s
msg.s
; 
#ERROR_NONE = 0
#KEY_WOW64_64KEY=$100
#KEY_WOW64_32KEY=$200

Procedure.l QueryValueEx(lhkey.l, szValueName.s) 
    Define.l cch, lrc, lType, lValue 
    Define.s sValue
    Shared vValue 
    cch = 255 
    sValue = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch) 
        
    Select lType
        Case #REG_SZ 
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch) 
            If lrc = #ERROR_NONE 
                vValue = Left(sValue, cch-1) 
            Else 
                vValue = "Empty" 
            EndIf
        Case #REG_DWORD 
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @lValue, @cch) 
            If lrc = #ERROR_NONE 
              vValue = Str(lValue) 
            EndIf 
        Default
            lrc = -1 
    EndSelect 
    ProcedureReturn lrc 
EndProcedure

      sRemMachName = ""
      If  OSVersion() >= 60 And OSVersion() < 70
        lTopLevelKey = #HKEY_CLASSES_ROOT
        sKeyName = "Http\Shell\Open\Command"
        sValueName = ""
      ElseIf OSVersion() >= 70 And OSVersion() < 100
        lTopLevelKey = #HKEY_CURRENT_USER
        sKeyName = "Software\Clients\StartMenuInternet"
        sValueName = ""
      EndIf
      
      lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle)      
      lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0,#KEY_READ|#KEY_WOW64_64KEY, @lhkey)      
      lRetVal = QueryValueEx(lhkey, sValueName)      
      RegCloseKey_(lhkey) 
      
      If lRetVal = 0
       If FindString(vValue,"FIREFOX.EXE",1) Or FindString(vValue,"firefox.exe",1) 
        MessageRequester("Default Browser","FireFox",#MB_ICONINFORMATION) 
       ElseIf FindString(vValue,"IEXPLORE.EXE",1) Or FindString(vValue,"iexplore.exe",1)
        MessageRequester("Default Browser","MS Internet Explorer",#MB_ICONINFORMATION)
       Else
        MessageRequester("Default Browser","Unknown",#MB_ICONINFORMATION)
       EndIf
      Else 
        msg = "An Error occured, Return value = " + Str(lRetVal)
       MessageRequester("Error",msg, #MB_ICONERROR) 
      EndIf
Egypt my love
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Get Windows Internet Default Browser

Post by rsts »

Hi Rashad,

On my windows 7 64 bit there is no #HKEY_CURRENT_USER\Software\Clients
thus I get a -1 error.

Not a complaint, just letting you know :)

cheers
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser

Post by RASHAD »

Hi rsts

Glad To hear from you
That is strange

Check the Next link And tell me what could be the cause

How To Register an Internet Browser Or E-mail Client With the Windows Start Menu
http://msdn.microsoft.com/en-us/librar ... .85).aspx

And with the real and good comments I am so happy
Back me up my friend

have a good day
Egypt my love
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Get Windows Internet Default Browser

Post by rsts »

Hi Rashad,

For my win 7 installation there is no #HKEY_CURRENT_USER\Software\Clients -
But there is a #HKEY_LOCAL_MACHINE\Software\Clients\StartMenuInternet

which correctly returns
---------------------------
Default Browser
---------------------------
MS Internet Explorer
---------------------------
OK
---------------------------
it may be necessary to check #HKEY_CURRENT_USER\Software\Clients first and if none check
#HKEY_LOCAL_MACHINE\Software\Clients\StartMenuInternet.

That's the way it would appear to work on mine.

cheers
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser

Post by RASHAD »

Good
I will do it
Your wish is a command mate

Have a nice day
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Windows Internet Default Browser

Post by RASHAD »

Update

Code: Select all

lRetVal.i
sRemMachName.s
lTopLevelKey.i
lHKeyhandle.i
sKeyName.s
lhkey.i
sValueName.s
vValue.s
msg.s
; 
#ERROR_NONE = 0
#KEY_WOW64_64KEY=$100
#KEY_WOW64_32KEY=$200

Procedure.l QueryValueEx(lhkey.l, szValueName.s) 
    Define.l cch, lrc, lType, lValue 
    Define.s sValue
    Shared vValue 
    cch = 255 
    sValue = Space(255)
    lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch) 
        
    Select lType
        Case #REG_SZ 
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch) 
            If lrc = #ERROR_NONE 
                vValue = Left(sValue, cch-1) 
            Else 
                vValue = "Empty" 
            EndIf
        Case #REG_DWORD 
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @lValue, @cch) 
            If lrc = #ERROR_NONE 
              vValue = Str(lValue) 
            EndIf 
        Default
            lrc = -1 
    EndSelect 
    ProcedureReturn lrc 
EndProcedure

      sRemMachName = ""
      sValueName = ""
      If  OSVersion() >= 60 And OSVersion() < 70
        lTopLevelKey = #HKEY_CLASSES_ROOT
        sKeyName = "Http\Shell\Open\Command"
        ;sValueName = ""
      ElseIf OSVersion() >= 70 And OSVersion() < 100
        lTopLevelKey = #HKEY_CURRENT_USER
        sKeyName = "Software\Clients\StartMenuInternet"
        ;sValueName = ""
      EndIf

Check2:      
      lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle)      
      lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0,#KEY_READ|#KEY_WOW64_64KEY, @lhkey)      
      lRetVal = QueryValueEx(lhkey, sValueName)      
      RegCloseKey_(lhkey)
      
      If OSVersion() >= 70 And OSVersion() < 100 And lRetVal <> 0
         lTopLevelKey = #HKEY_LOCAL_MACHINE
         sKeyName = "Software\Clients\StartMenuInternet"
         Goto Check2
      EndIf
      
      If lRetVal = 0
       If FindString(vValue,"FIREFOX.EXE",1) Or FindString(vValue,"firefox.exe",1) 
        MessageRequester("Default Browser","FireFox",#MB_ICONINFORMATION) 
       ElseIf FindString(vValue,"IEXPLORE.EXE",1) Or FindString(vValue,"iexplore.exe",1)
        MessageRequester("Default Browser","MS Internet Explorer",#MB_ICONINFORMATION)
       Else
        MessageRequester("Default Browser","Unknown",#MB_ICONINFORMATION)
       EndIf
      Else 
        msg = "An Error occured, Return value = " + Str(lRetVal)
       MessageRequester("Error",msg, #MB_ICONERROR) 
      EndIf
Egypt my love
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 130
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: Get Windows Internet Default Browser

Post by CONVERT »

Thanks Rashad,

I used your first code:

Code: Select all

Procedure.s get_default_internet_browser ()
  Define wresult$, wno_test, wftest$
  
  wresult$ = Space(1024)
  wftest$ = "test.html"
  
  wno_test = CreateFile(#PB_Any,wftest$)
  If wno_test <> 0
    CloseFile(wno_test)

    FindExecutable_(wftest$,GetCurrentDirectory(),wresult$)
    DeleteFile(wftest$)
  EndIf
  ProcedureReturn Trim(wresult$)
EndProcedure

MessageRequester("",get_default_internet_browser ())
PureBasic 6.20 beta 2 (x64) | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled.
Come back to 6.11 LTS 64 bits because of an issue with #PB_ComboBox_UpperCase in ComboBoxGadget() (Oct. 10, 2024).
Post Reply