Delphi coding hints and tips.
Launching applications within Delphi apps
To launch an application or execute a file use the ShellAPI function ShellExecute. With ShellExecute you can open a specific application with given parameters. Or you can open a file and the associated app will be opened with this file. This is like double clicking a file within explorer. Below are some examples of calling ShellExecute:-
-
Open notepad.exe passing parameters c:\config.sys effectively opening config.sys in notepad
ShellExecute(Handle,'open', 'notepad','c:\config.sys','',SW_SHOWNORMAL);
-
Open web site www.haydenr.com using your default browser app.
ShellExecute(Handle,'open', 'http://www.haydenr.com','','',SW_SHOWNORMAL);
-
Open any document specified in variable StrFileName. The handle of the application started to execute doc is returned and stored.
var AppHandle:HWND; AppHandle:=ShellExecute(Handle,'open', PChar(StrFileName),'','',SW_SHOWNORMAL);
-
Prints the readme.txt file.
ShellExecute(Handle,'print', 'c:\windows\readme.txt','','',SW_SHOWNORMAL);
Check out the help on ShellExecute for full description of parameters and error codes returned.
If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application. If the function fails, the return value is an error value.
Note: You need to add ShellAPI to the uses statement of unit.
Note: All calls to ShellExecute use null terminated strings so cast an Strings as a PChar.


