[Delphi (Object Pascal)] Delphi通过管道执行外部命令行程序(cmd)并获取返回结果 →→→→→进入此内容的聊天室

来自 , 2019-04-20, 写在 Delphi (Object Pascal), 查看 115 次.
URL http://www.code666.cn/view/297b631a
  1. //================================================================
  2. //K8执行DOS并返回结果
  3. function RunDosCommand(Command: string): string;
  4. var
  5.   hReadPipe: THandle;
  6.   hWritePipe: THandle;
  7.   SI: TStartUpInfo;
  8.   PI: TProcessInformation;
  9.   SA: TSecurityAttributes;
  10.   //     SD   :   TSecurityDescriptor;
  11.   BytesRead: DWORD;
  12.   Dest: array[0..1023] of char;
  13.   CmdLine: array[0..512] of char;
  14.   TmpList: TStringList;
  15.   Avail, ExitCode, wrResult: DWORD;
  16.   osVer: TOSVERSIONINFO;
  17.   tmpstr: AnsiString;
  18. begin
  19.   osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);
  20.   GetVersionEX(osVer);
  21.  
  22.   if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then
  23.   begin
  24.   //         InitializeSecurityDescriptor(@SD,   SECURITY_DESCRIPTOR_REVISION);
  25.   //         SetSecurityDescriptorDacl(@SD,   True,   nil,   False);
  26.     SA.nLength := SizeOf(SA);
  27.     SA.lpSecurityDescriptor := nil; //@SD;
  28.     SA.bInheritHandle := True;
  29.     CreatePipe(hReadPipe, hWritePipe, @SA, 0);
  30.   end
  31.   else
  32.     CreatePipe(hReadPipe, hWritePipe, nil, 1024);
  33.   try
  34.     FillChar(SI, SizeOf(SI), 0);
  35.     SI.cb := SizeOf(TStartUpInfo);
  36.     SI.wShowWindow := SW_HIDE;
  37.     SI.dwFlags := STARTF_USESHOWWINDOW;
  38.     SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
  39.     SI.hStdOutput := hWritePipe;
  40.     SI.hStdError := hWritePipe;
  41.     StrPCopy(CmdLine, Command);
  42.     if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
  43.     begin
  44.       ExitCode := 0;
  45.       while ExitCode = 0 do
  46.       begin
  47.         wrResult := WaitForSingleObject(PI.hProcess, 500);
  48.   //                 if   PeekNamedPipe(hReadPipe,   nil,   0,   nil,   @Avail,   nil)   then
  49.         if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
  50.         begin
  51.           if Avail > 0 then
  52.           begin
  53.             TmpList := TStringList.Create;
  54.             try
  55.               FillChar(Dest, SizeOf(Dest), 0);
  56.               ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
  57.               TmpStr := Copy(Dest, 0, BytesRead - 1);
  58.               TmpList.Text := TmpStr;
  59.               Result := tmpstr;
  60.             finally
  61.               TmpList.Free;
  62.             end;
  63.           end;
  64.         end;
  65.         if wrResult <> WAIT_TIMEOUT then ExitCode := 1;
  66.       end;
  67.       GetExitCodeProcess(PI.hProcess, ExitCode);
  68.       CloseHandle(PI.hProcess);
  69.       CloseHandle(PI.hThread);
  70.     end;
  71.   finally
  72.     CloseHandle(hReadPipe);
  73.     CloseHandle(hWritePipe);
  74.   end;
  75. end;
  76. //delphi/8999

回复 "Delphi通过管道执行外部命令行程序(cmd)并获取返回结果"

这儿你可以回复上面这条便签

captcha