Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I execute another program from my Delphi Application?

Delphi with Other Programs

How do I execute another program from my Delphi Application?

by  towerbase  Posted    (Edited  )

This is surprisingly easy to do.

First ensure that ShellApi is in a uses statement.
Code:
Uses ShellApi;
Then call ShellExecute. This function takes six parameters but at its simplest it can be called as follows:
Code:
  ShellExecute(0, 'Open', PChar(App), PChar(''), PChar(''), SW_Hide);
Where App is the full path name of the application you wish to call OR it is the name of a file which is associated with an application.

Examples:

If you wish to start an application called 'MyApp.exe' from within your Delphi application then you would code something like
Code:
  ShellExecute(0, 'Open', PChar('c:\Apps\MyApp.exe'), PChar(''), PChar(''), SW_Hide);

If you have Microsoft Word installed on your computer then it is likely that
Code:
  ShellExecute(0, 'Open', PChar('c:\My Documents\Biography.doc'), PChar(''), PChar(''), SW_Hide);
would start Word and open your document called Biography.doc.

Parameter 4 allows you to pass parameters to your program. For example, if your program required two digits as parameters then you would code something like:-
Code:
  ShellExecute(0, 'Open', PChar('c:\Apps\MyApp.exe'), PChar('123 999'), PChar(''), SW_Hide);
Parameter 5 allows you to specify a default directory to your application. For example, your program might require a temporary directory:
Code:
  ShellExecute(0, 'Open', PChar('c:\Apps\MyApp.exe'), PChar('123 999'), PChar('c:\temp'), SW_Hide);
Note that the application that calls ShellExecute does not suspend whilst the called program is running. Both programs will be executing at the same time. The next FAQ (coming shortly) will show you how to suspend your program until the called program completes.

The Windows SDK Help provides details of what each of the parameters are used for and what the returned value from ShellExecute means.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top