Visual Basic Internet Routines
FindExecutable: Obtain Exe of the Default Browser
Posted:   Wednesday October 13, 1999
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch


CreateProcess: Start Separate Instances of the Default Browser
FindExecutable: Find Exe Associated with a Registered Extension
RegSetValueEx: Create a Registered File Association

The routine here will return the full drive, path and filename of the user's default application associated with html files. Typically, this will always be the user's default browser.

The routine works by first determining the system temp folder, creating a dummy filename there with the extension .html, then passing that information to FindExecutable. The API returns the path and application associated with the passed file .html extension, and the temporary file is deleted. Also passed to the function is a flags parameter which is filled with the return code of the API call. The return value can be handled as needed by the app.

The path/application is returned as a short or long filename as exactly as stored in the registry.

Note however that this code can not guarantee the associated application returned is a browser, only that *some* application is associated with the specified extension. While in most cases this will be the default browser, it is possible that another browser or application has hijacked the html extension.  One reported case indicated MS Word was associated with the html file extension.  Therefore it may be justified to check the returned string from this call for "iexplore.exe" or the name of the browser of your choice, and if not the expected value to prompt the user to select their default browser, which you'd then save as your own setting for future reference. Another alternative would be to present the user with the list of installed browsers contained under the registry key:


... or to retrieve the application related to the http:// command listed under:


 BAS Module Code

 Form Code
Drop a command button onto a form and add the following:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function FindExecutable Lib "shell32" _
   Alias "FindExecutableA" _
  (ByVal lpFile As String, _
   ByVal lpDirectory As String, _
   ByVal sResult As String) As Long

Private Declare Function GetTempPath Lib "kernel32" _
   Alias "GetTempPathA" _
  (ByVal nSize As Long, _
   ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260
Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
Private Const ERROR_FILE_NOT_FOUND As Long = 2
Private Const ERROR_PATH_NOT_FOUND As Long = 3
Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
Private Const ERROR_BAD_FORMAT As Long = 11

Private Sub Command1_Click()

   Dim success As Long
   Dim sBrowser As String
  'success is passed and filled in the routine
   sBrowser = GetBrowserName(success)
  'possible return values from the call
  'returned in success
   Select Case success
     'the call succeeded
         MsgBox sBrowser
         Exit Sub
     'other possible return values
      Case Else
   End Select
  'if this far the call failed
   MsgBox "No dice!"
End Sub

Private Function GetBrowserName(dwFlagReturned As Long) As String

   Dim hFile As Long
   Dim sResult As String
   Dim sTempFolder As String
  'get the user's temp folder
   sTempFolder = GetTempDir()
  'create a dummy html file in the temp dir
   hFile = FreeFile
      Open sTempFolder & "dummy.html" For Output As #hFile
   Close #hFile

  'get the file path & name associated with the file
   sResult = Space$(MAX_PATH)
   dwFlagReturned = FindExecutable("dummy.html", sTempFolder, sResult)
  'clean up
   Kill sTempFolder & "dummy.html"
  'return result
   GetBrowserName = TrimNull(sResult)
End Function

Private Function TrimNull(item As String)

    Dim pos As Integer
    pos = InStr(item, Chr$(0))
    If pos Then
       TrimNull = Left$(item, pos - 1)
       TrimNull = item
    End If
End Function

Public Function GetTempDir() As String

    Dim nSize As Long
    Dim tmp As String
    tmp = Space$(MAX_PATH)
    nSize = Len(tmp)
    Call GetTempPath(nSize, tmp)
    GetTempDir = TrimNull(tmp)
End Function
This method returns the path and filename of the associated executable stored in the registry. For example, the above call (on my system) returns E:\PROGRA~1\INTERN~1\iexplore.exe, as opposed to the long path name.

FindExecutable returns a value greater than 32 if successful, or a value less than or equal to 32 otherwise. The following table lists the possible error values:

0 The system is out of memory or resources.
31 There is no association for the specified file type.
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.
ERROR_BAD_FORMAT The .exe file is invalid (non-Win32 .exe or error in .exe image).

In addition, the MSDN Remarks section for FindExecutable states that when FindExecutable returns, the lpResult parameter may contain the path to the DDE (Dynamic Data Exchange) server started if a server does not respond to a request to initiate a DDE conversation with the DDE client application.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter