Visual Basic System Services
FindExecutable: Find Exe Associated with a Registered Extension
Posted:   Sunday September 12, 1999
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch


ShellExecute: Simulate a Hyperlink with a Label Control
FindExecutable: Find Exe Associated with a Registered Extension
ShellExecute: ShellExecute Madness
CreateProcess: Start Separate Instances of the Default Browser

ShellExecute: Send Large Emails in Outlook Express

While the ShellExecute and ShellExecuteEx APIs provide direct means to launch an application by passing an associated file, on occasion developers may need to know exactly which application/executable a particular file association is tied to. Calling the FindExecutable API, and passing a valid file name and path, returns the path and application associated with the passed file extension.

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

 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 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 pos As Long
   Dim sResult As String
   Dim msg As String
   sResult = Space$(MAX_PATH)

  'lpFile: name of the file of interest
  'lpDirectory: location of lpFile
  'sResult: path and name of executable associated with lpFile
   success = FindExecutable("winhlp32.hlp", "c:\winnt\system32\", sResult)
   Select Case success
      Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
      Case ERROR_FILE_NOT_FOUND: msg = "file not found"
      Case ERROR_PATH_NOT_FOUND: msg = "path not found"
      Case ERROR_BAD_FORMAT:     msg = "bad format"
      Case Is >= ERROR_FILE_SUCCESS:
         pos = InStr(sResult, Chr$(0))
         If pos Then
            msg = Left$(sResult, pos - 1)
         End If
   End Select
   MsgBox msg
End Sub
This method returns the path and filename of the associated executable. It does not return the "friendly" name. For example, the above call (on my system) returns c:\winnt\system32\winhlp32.exe, not "Windows NT Help". Passing a html file returns e:\progra~1\intern~1\iexplore.exe, not "Microsoft Internet Explorer".

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