Article ID:qGEN010
Date Revised:July 28, 2000
Keywords:Win32API, ShellExecute, API, Web, Browser, file extension, application
See Also:SpyIn Utility   KB Article Q114038   KB Article Q170918   

Question: How can I launch the default application for a given file?

Answer: The API function ShellExecute can be used to launch the other application from within VFP.

NOTE: The file dfwinapi.vcx in the SpyIn.ZIP provides an object wrapper around the API call to make it much easier to use.

declare long ShellExecute in "shell32.dll" long hwnd, string lpszOp, ;
                    string lpszFile, string lpszParams, ;
                    string lpszDir, long nShowCmd

declare long GetDesktopWindow in"win32api"

local hDCScreen

hDCScreen = GetDesktopWindow()

Since the file doesn't exist notepad will ask you if you want to create it, answer Yes put something in the file and close notepad

? ShellExecute( hDCScreen, "open", "notepad.exe", "c:\testshellexecute.txt", "c:\", 1 )

Now that the file exists we can simply give it a filename and Windows will open the program associated with .TXT files. If you've changed this from notepad.exe the other program will run, not notepad.

? ShellExecute( hDCScreen, "open", "c:\testshellexecute.txt", "", "c:\", 1 )

Launch a help file

? ShellExecute( hDCScreen, "open", "C:\WINDOWS\HELP\SOL.HLP", "", "c:\", 1 )

Open the sound recorder

? ShellExecute( hDCScreen, "open", "C:\WINDOWS\MEDIA\The Microsoft Sound.wav", "", "c:\", 1 )

Open the sound recorder, play the file and close

? ShellExecute( hDCScreen, "play", "C:\WINDOWS\MEDIA\The Microsoft Sound.wav", "", "c:\", 1 )

Launch the default browser

? ShellExecute( hDCScreen, "open", "http://www.microsoft.com", "", "c:\", 1 )

Open an existing document file (file must exist first)

? ShellExecute( hDCScreen, "open", "MyFile.doc", "", "c:\", 1 )

Open an existing Excel spreadsheet (file must exist first)

? ShellExecute( hDCScreen, "open", "MySpreadSheet.xls", "", "c:\", 1 )

Open your default email client

? ShellExecute( hDCScreen, "open", "mailto:DavidFrankenbach@worldnet.att.net", "", "c:\", 1 )

Run a DOS command window

? ShellExecute( hDCScreen, "open", "command.com", "", "c:\", 1 )

Use a DOS internal command

? ShellExecute( hDCScreen, "open", "command.com", "/k dir c:\*.*", "c:\", 1 )

Use a DOS internal command routing it's output to a file

? ShellExecute( hDCScreen, "open", "command.com", "/c dir c:\*.* >>test.txt", "c:\", 1 )

Run a DOS external command

? ShellExecute( hDCScreen, "open", "xcopy", "c:\autoexec.bat c:\autoexec.tst", "c:\", 1 )

Run the Desktop control panel applet

? ShellExecute( hDCScreen, "open", "control.exe", "desk.cpl,@0,3", "", 1 )


1