Delphi Tips and Code
Managing an External Application within Delphi
It is often useful to be able to start another program from within your delphi application and then wait for the other program to finish. These was done using "executeandwait" but this has now been replaced by the Windows API "Createprocess". The following example shows how to use a procedure to run the external program. The advantage of doing it this way is the produce is defined once (often in a separate unit) and can then be called whenever needed in your applcation. This will work as advertised in all modern versions of Delphi.
Insert the following declarations in the "var" section of the Interface section of the unit.
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
The following function is what does the work. It makes a call to the CreateProcess API. See the win32 help that comes with Delphi or MSDN for details on the meaning of the parameters being used.
function TForm.RunProgram(CmdLine: string): string;
begin
//a function that uses createprocess windows API to run the program
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(StartupInfo);
CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil,
StartupInfo, ProcessInfo);
CloseHandle(ProcessInfo.hThread);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
end;
Now all you need to do to run the external application is insert the following:
RunProgram('[path]\external program name');
e.g. RunProgram('C:\windows\notepad.exe');