您的位置:首页 > 编程语言 > Delphi

delphi调用外部EXE,等待执行完毕时再响应后续操作.

2012-11-24 09:37 309 查看
http://blog.csdn.net/gjsshn_2010/article/details/4158360

function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PDWORD): Longword;

var

StartupInfo: TStartupInfo;

ProcessInfo: TProcessInformation;

begin

FillChar(StartupInfo, SizeOf(StartupInfo), #0);

StartupInfo.cb := SizeOf(StartupInfo);

StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;

StartupInfo.wShowWindow := ShowCmd;

if not CreateProcess(nil,@Filename[1], nil, nil, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo,ProcessInfo)

then

Result := WAIT_FAILED

else

begin

if wait = FALSE then

begin

if ProcID <> nil then

ProcID^ := ProcessInfo.dwProcessId;

result := WAIT_FAILED;

exit;

end;

WaitForSingleObject(ProcessInfo.hProcess, INFINITE);

GetExitCodeProcess(ProcessInfo.hProcess, Result);

end;

if ProcessInfo.hProcess <> 0 then

CloseHandle(ProcessInfo.hProcess);

if ProcessInfo.hThread <> 0 then

CloseHandle(ProcessInfo.hThread);

end;

procedure TForm1.Button1Click(Sender: TObject);

var

ProcID: Cardinal;

begin

ProcID := 0;

if OpenDialog1.Execute then

RunProcess(OpenDialog1.FileName, SW_SHOW, CheckBox1.Checked, @ProcID);

ShowMessage(IntToStr(ProcID));

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: