Running an EXE file via a Delphi App

Running an EXE file via a Delphi App

I want to be able to run an EXE file on a button click in one of my Delphi applications. Is it possible to do this? Where do I declare the file I want to run, and how do I put it into basic code?

If you’re running Win16, then the call to make is WinExec. For Win32 environments, you can use ShellExecute. But you really should use CreateProcess. Here’s some sample code that I use to execute external programs:

{Supply a fully qualified path name in ProgramName and any arguments on the command line. As the  help file states: "If lpApplicationName is NULL,  the first white space-delimited token of the  command line specifies the module name..." In  English, the characters before the first space  encountered (or if no space is encountered as in  a single program call) is interpreted as the EXE to execute. The rest of the string is the  argument line.} procedure ExecNewProcess(ProgramName : String);var  StartInfo  : TStartupInfo;  ProcInfo   : TProcessInformation;  CreateOK   : Boolean;begin  { fill with known state }  FillChar(StartInfo,SizeOf(TStartupInfo),#0);  FillChar(ProcInfo,SizeOf(TProcessInformation),           #0);  StartInfo.cb := SizeOf(TStartupInfo);  CreateOK := CreateProcess(nil,               PChar(ProgramName), nil, nil,              False,              CREATE_NEW_PROCESS_GROUP +               NORMAL_PRIORITY_CLASS,               nil, nil, StartInfo, ProcInfo);  { check to see if successful }  if CreateOK then    //may or may not be needed. Usually wait for     //child processes. You can comment this line    //out.    WaitForSingleObject(ProcInfo.hProcess,                         INFINITE);end;

The function above only works for Win32 programs, it will not work for Win16–you’ll get a compiler error.


Share the Post: