2018-05-08 22:07:29
Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/76
Sometimes, we need to run another program in our program, and to perform some actions only after the program runs to completion.
Here is how:
procedure FlushMouseAndKeyboardWindowsMessages;
var
Msg : TMsg;
begin
while PeekMessage( Msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE) do;
while PeekMessage( Msg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do;
end;
procedure RunAnotherProgramAndWait(aProgram: string; aCmdLineParameters: string);
var
_startInfo: TStartupInfo;
_processInfo: TProcessInformation;
_res :cardinal;
begin
FillChar(_startInfo, SizeOf(_startInfo), #0);
_startInfo.cb := SizeOf(_startInfo);
_startInfo.dwFlags := STARTF_USESHOWWINDOW;
_startInfo.wShowWindow := SW_SHOWNORMAL;
if CreateProcess(PChar(aProgram),
PChar(aCmdLineParameters),
Nil, // pointer to process security attributes
Nil, // pointer to thread security attributes
False, // handle inheritance flag
0, // creation flags
Nil, // pointer to new environment block
Nil, // pointer to current directory name
_startInfo, // pointer to STARTUPINFO
_processInfo // pointer to PROCESS_INFORMATION
) then
begin
//Wait for it to terminate
repeat
FlushMouseAndKeyboardWindowsMessages;
application.ProcessMessages;
_res := MsgWaitForMultipleObjects(1, _processInfo.hProcess, False,
INFINITE, QS_PAINT);
until
_res = WAIT_OBJECT_0;
end;
end;