How To Find the Full Path of an Executable Given the Extension

This article was previously published under Q197192
This article has been archived. It is offered "as is" and will no longer be updated.
Sometimes you might need to determine the full pathname to a Windowsexecutable file, such as Microsoft Word or Excel. Passing a file with thedesired extension, be it .DOC, .XLS, .PPT, etc. to the Windows API functionFindExecutable is one easy way to do this. You can use this function inboth 16-bit FoxPro for Windows and 32-bit Visual FoxPro.
Save and execute the following program, FindExec.prg. FindExec.prg createsa dummy file named Sample.doc and uses this file to find the full path toMicrosoft Word. The filename can be altered to find the executableassociated with any registered file type.

FindExecutable returns an integer value greater than 32 when it issuccessful. Other error codes indicate that no association exists, that thespecified file or path does not exist, or that the function failed becauseof insufficient memory.

Sample Code

   * FindExec.prg   *   * This example demonstrates Visual FoxPro and FPW2.6 code to find the   * location of Word for Windows based on a file association.   *   * A file with the specific association must already exist for   * FindExecutable to work correctly. Therefore, the sample creates   * a fictitious file with a .DOC extension.   *   * If an association is not present, FindExecutable will   * return an empty string.   * Modify this string to SAMPLE.XLS to find the path to Excel.   lcSampleDoc = "SAMPLE.DOC"   lihandle = FCREATE(lcSampleDoc)   =FCLOSE(lihandle)   lpFile = lcSampleDoc   lpDirectory = ''   lpResults = SPACE(128)   * Any VFP version.   IF ("VISUAL FOXPRO" $ UPPER(VERSION()))      DECLARE INTEGER FindExecutable IN SHELL32 ;         STRING@lpFile, STRING@lpDirectory, ;         STRING @lpResults      liReturnValue = FindExecutable(@lpFile, @lpDirectory,@lpResults)   ELSE   * FPW 2.x version.      SET LIBRARY TO HOME() + "foxtools.fll"      lfFindExe = RegFn("FindExecutable", "@C@C@C", "I", ;         "SHELL.DLL")      liReturnValue = CallFn(lfFindExe, @lpFile, @lpDirectory, @lpResults)   ENDIF   * Interpret the return code.   DO CASE   CASE liReturnValue = 0      ? "FindExecutable failed: Out of memory or resources"   CASE liReturnValue = 31      ? "FindExecutable failed: No association for file type"   CASE liReturnValue = 2      ? "FindExecutable failed: Specified file not found"   CASE liReturnValue = 3      ? "FindExecutable failed: Specified path not found"   CASE liReturnValue = 11      ? "FindExecutable failed: Invalid EXE format"   OTHERWISE   * Read out to the null terminator.      lpResults = LEFT(lpResults, AT(CHR(0), lpResults) - 1)      ? "Full path of application: " + lpResults   ENDCASE   * Delete the dummy file you created.   DELETE FILE (lcSampleDoc)				
Win32 SDK Help; topic: FindExecutable

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
140724 PRB: FindExecutable() Truncates Result at First Space in LFN

Article ID: 197192 - Last Review: 02/21/2014 00:31:10 - Revision: 2.2

  • Microsoft FoxPro 2.6 Standard Edition
  • Microsoft FoxPro 2.6a Standard Edition
  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 3.0b Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • kbnosurvey kbarchive kbapi kbhowto KB197192