使用下面的函数
function CreateProcessAndWait(const AppParams: String; Visibility: word;
ProcessMessage: boolean = true;
WaitTime: integer = 10000): DWord;
var
SI: TStartupInfo;
PI: TProcessInformation;
Proc: THandle;
MaxWaitTime: integer;
begin
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
SI.wShowWindow := Visibility;
// Must send NULL for the first Param if DOS app
if (not CreateProcess(nil, PChar(AppParams),
nil, nil,
FALSE, NORMAL_PRIORITY_CLASS,
nil, PChar(ExtractFilePath(AppParams)),
SI, PI) ) then
raise Exception.CreateFmt('无法执行程序: ' + AppParams + ' 错误代码: %d', [GetLastError]);
Proc := PI.hProcess;
CloseHandle(PI.hThread);
MaxWaitTime := WaitTime div 50;
while (WaitForSingleObject(Proc, 50) = WAIT_TIMEOUT) and ( MaxWaitTime > 0 ) do
begin
if ProcessMessage then Application.ProcessMessages;
Dec(MaxWaitTime);
end;