用这个函数:WinExecAndWait,等待程序执行完毕才返回:
Path是要执行的文件名,Visibility表示是否可见,值见Win32API:ShowWindow的第二个参数。
function WinExecAndWait(Path: string; Visibility: word): DWord;
var
P: integer;
Params: string;
begin
P := Pos(' ', Path); // assume params start at first space
Params := Copy(Path, P, Length(Path));
Delete(Path, P, Length(Path));
Result := CreateProcessAndWait(Path, Params, Visibility);
end;
function CreateProcessAndWait(const AppPath, AppParams: string;
Visibility: word): DWord;
var
SI: TStartupInfo;
PI: TProcessInformation;
Proc: THandle;
begin
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
SI.wShowWindow := Visibility;
if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, False,
Normal_Priority_Class, nil, nil, SI, PI) then
raise Exception.CreateFmt('不能执行,请输入或选择可执行文件的名称和路径.返回的错误码为%d',
[GetLastError]);
Proc := PI.hProcess;
CloseHandle(PI.hThread);
if WaitForSingleObject(Proc, Infinite) <> Wait_Failed then
GetExitCodeProcess(Proc, Result)
else
Application.ProcessMessages;
CloseHandle(Proc);
end;