function RunProgram(ProgramName:string;Wait:Boolean=False):Cardinal; var StartInfo:STARTUPINFO; ProcessInfo:PROCESS_INFORMATION; begin //执行外部程序,失败返回0,成功返回进程句柄 Result:=0; if ProgramName='' then exit; GetStartupInfo(StartInfo); StartInfo.dwFlags:=StartInfo.dwFlags or STARTF_FORCEONFEEDBACK; if not CreateProcess(nil,PChar(ProgramName),nil,nil,false,0, nil,nil,StartInfo,ProcessInfo) then exit; Result:=ProcessInfo.hProcess; //建立进程成功 //如果异步执行则退出 if not wait then exit; while IsProgram_Runing(Result) do Application.ProcessMessages; end;function IsProgram_Runing(hProcess:Cardinal):Boolean; var ExitCode:Cardinal; begin //查看进程是否正在运行 GetExitCodeProcess(hProcess,ExitCode); if ExitCode=STILL_ACTIVE then Result:=True else Result:=False; end;
var
StartInfo:STARTUPINFO;
ProcessInfo:PROCESS_INFORMATION;
begin
//执行外部程序,失败返回0,成功返回进程句柄
Result:=0;
if ProgramName='' then exit;
GetStartupInfo(StartInfo);
StartInfo.dwFlags:=StartInfo.dwFlags or STARTF_FORCEONFEEDBACK;
if not CreateProcess(nil,PChar(ProgramName),nil,nil,false,0,
nil,nil,StartInfo,ProcessInfo) then exit;
Result:=ProcessInfo.hProcess;
//建立进程成功
//如果异步执行则退出
if not wait then exit;
while IsProgram_Runing(Result) do Application.ProcessMessages;
end;function IsProgram_Runing(hProcess:Cardinal):Boolean;
var
ExitCode:Cardinal;
begin
//查看进程是否正在运行
GetExitCodeProcess(hProcess,ExitCode);
if ExitCode=STILL_ACTIVE then
Result:=True
else
Result:=False;
end;
同步调用方法 RunProgram('c:\aa.exe',true);参考IsProgram_Runing函数,你也可以RunProgram('c:\aa.exe');先异步执行,然后根据返回的进程句柄,自己判断程序是否执行结束。