执行命令行并等待完成

function TDMDb.WaitExeFinish(const sCmdName: string):boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  b:boolean;
begin
  // 初始化
  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_HIDE; // 隐藏执行


  // 执行命令行
  b:=CreateProcess(nil, PWideChar(sCmdName), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo);


  // 等待执行完成
  with ProcessInfo do
  begin
    CloseHandle(hThread);
    WaitForSingleObject(hProcess, INFINITE);
    CloseHandle(hProcess);
  end;


  // 返回
  Result := b;
end;

你可能感兴趣的:(执行命令行并等待完成)