执行程序函数

执行程序及启动参数, 并可选是否等待程序结束

function RunApp(AApp, AParams: String; AWaitFor: Boolean): DWORD;
var
  nSI: TSTARTUPINFO;
  nPI: TPROCESSINFORMATION;
  nR: Boolean;
begin
  Result := 0;
  with nSI do
  begin
    cb := Sizeof(TSTARTUPINFO);
    lpReserved := nil;
    lpDesktop := nil;
    lpTitle := nil;
    dwFlags := STARTF_USESHOWWINDOW and STARTF_USESTDHANDLES;
    cbReserved2 := 0;
    lpReserved2 := nil;
  end;
  nR := CreateProcess(PChar(AApp), PChar(AParams), nil, nil, True,
    CREATE_DEFAULT_ERROR_MODE and NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(AApp)), nSI,
    nPI);
  if nR then { 等待结束 }
  begin
    if AWaitFor then
    begin
      Result := 1;
      WaitForSingleObject(nPI.hProcess, INFINITE);
      CloseHandle(nPI.hProcess); { 清理句柄 }
      CloseHandle(nPI.hThread);
    end
    else
      Result := nPI.dwProcessId;
  end;
end;

 

posted on 2017-10-12 16:00 堕落恶魔 阅读(...) 评论(...) 编辑 收藏

统计