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

Article translations Article translations
Article ID: 197192 - View products that this article applies to.
This article was previously published under Q197192
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

SUMMARY

Sometimes you might need to determine the full pathname to a Windows executable file, such as Microsoft Word or Excel. Passing a file with the desired extension, be it .DOC, .XLS, .PPT, etc. to the Windows API function FindExecutable is one easy way to do this. You can use this function in both 16-bit FoxPro for Windows and 32-bit Visual FoxPro.

MORE INFORMATION

Save and execute the following program, FindExec.prg. FindExec.prg creates a dummy file named Sample.doc and uses this file to find the full path to Microsoft Word. The filename can be altered to find the executable associated with any registered file type.

FindExecutable returns an integer value greater than 32 when it is successful. Other error codes indicate that no association exists, that the specified file or path does not exist, or that the function failed because of 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)
				

REFERENCES

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

Properties

Article ID: 197192 - Last Review: February 21, 2014 - Revision: 2.2
APPLIES TO
  • 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
Keywords: 
kbnosurvey kbarchive kbapi kbhowto KB197192

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com