Retrieve name of EXE associated with file extension

Tagged:

MSDN: FindExecutable Function

This is sample code. Add error handling and adjust to your requirements as necessary.

#DEFINE MAX_PATH   260
 
DECLARE INTEGER FindExecutable IN Shell32 ;
	STRING lpFile, STRING lpDirectory, STRING @lpResult
 
&& File extension for which to find associated program
lcFileExt = ".pdf"
&& A file with specified extension is required. Creating temporary one.
lcTempFile = ADDBS(SYS(2023)) + SYS(2015) + lcFileExt
STRTOFILE("*", lcTempFile )
lcBuffer = SPACE(MAX_PATH)
lnExeHandle= FindExecutable(lcTempFile, "", @lcBuffer)
DO CASE
CASE lnExeHandle > 32
	lcExeName =  LEFT(lcBuffer, AT(CHR(0), lcBuffer)-1)
CASE lnExeHandle= 31
	&& No program associated with this extension
	lcExeName = ""
	? "There is no association for the specified file type"
OTHERWISE
	&& Some other error
	lcExeName = ""
	? lnResult
ENDCASE
 
? lcExeName
ERASE (lcTempFile)