2017年9月7日 星期四

執行外部程式 CreateProcess

function fn_CreateProcess(const AFileName, AParams: string; AWindowState: Word; AWaiting:Boolean=False): Boolean; //執行外部程式
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  //Ex: ExecAndWait(FileName, Param, SW_SHOW);
  CmdLine := '"' + AFileName + '"' + AParams;
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := AWindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
  CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS , nil, PChar(ExtractFilePath(AFileName)), SUInfo, ProcInfo);
  if Result then
  begin
    if AWaiting then
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
    CloseHandle(ProcInfo.hProcess);
    CloseHandle(ProcInfo.hThread);
  end;
end;

沒有留言:

張貼留言