zoukankan      html  css  js  c++  java
  • Delphi通过管道执行外部命令行程序(cmd)并获取返回结果

    该代码片段来自于: http://www.sharejs.com/codes/delphi/8999,发现好多代码,想用的时候找不到,记录一下备用

    function RunDosCommand(Command: string): string;
    var
      hReadPipe: THandle;
      hWritePipe: THandle;
      SI: TStartUpInfo;
      PI: TProcessInformation;
      SA: TSecurityAttributes;
      //     SD   :   TSecurityDescriptor;
      BytesRead: DWORD;
      Dest: AnsiString;
      TmpList: TStringList;
      Avail, ExitCode, wrResult: DWORD;
      osVer: TOSVERSIONINFO;
      tmpstr: AnsiString;
    begin
      SetLength(Dest, 1024);
      osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);
      GetVersionEX(osVer);

      if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then
      begin
      //         InitializeSecurityDescriptor(@SD,   SECURITY_DESCRIPTOR_REVISION);
      //         SetSecurityDescriptorDacl(@SD,   True,   nil,   False);
        SA.nLength := SizeOf(SA);
        SA.lpSecurityDescriptor := nil; //@SD;
        SA.bInheritHandle := True;
        CreatePipe(hReadPipe, hWritePipe, @SA, 0);
      end
      else
        CreatePipe(hReadPipe, hWritePipe, nil, 1024);
      try
        FillChar(SI, SizeOf(SI), 0);
        SI.cb := SizeOf(TStartUpInfo);
        SI.wShowWindow := SW_HIDE;
        SI.dwFlags := STARTF_USESHOWWINDOW;
        SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
        SI.hStdOutput := hWritePipe;
        SI.hStdError := hWritePipe;
        if CreateProcess(nil, PChar(@Command[1]), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
        begin
          ExitCode := 0;
          while ExitCode = 0 do
          begin
            wrResult := WaitForSingleObject(PI.hProcess, 500);
      //                 if   PeekNamedPipe(hReadPipe,   nil,   0,   nil,   @Avail,   nil)   then
            if PeekNamedPipe(hReadPipe, @Dest[1], 1024, @Avail, nil, nil) then
            begin
              if Avail > 0 then
              begin
                TmpList := TStringList.Create;
                try
                  FillChar(Dest[1], Length(Dest) * SizeOf(Char), 0);
                  ReadFile(hReadPipe, Dest[1], Avail, BytesRead, nil);
                  TmpStr := Copy(Dest, 0, BytesRead - 1);
                  TmpList.Text := TmpStr;
                  Result := tmpstr;
                finally
                  TmpList.Free;
                end;
              end;
            end;
            if wrResult <> WAIT_TIMEOUT then ExitCode := 1;
          end;
          GetExitCodeProcess(PI.hProcess, ExitCode);
          CloseHandle(PI.hProcess);
          CloseHandle(PI.hThread);
        end;
      finally
        CloseHandle(hReadPipe);
        CloseHandle(hWritePipe);
      end;
    end;
    //该代码片段来自于: http://www.sharejs.com/codes/delphi/8999, 并进行了适当的修改

  • 相关阅读:
    007_在线解析json工具
    009_python魔法函数
    008_python列表的传值与传址
    008_python内置语法
    007_Python中的__init__,__call__,__new__
    006_Python 异常处理
    匹配网络设计
    Bessel函数
    system generator 卷积编码器快速设计
    关于非稳恒的电流激励电场
  • 原文地址:https://www.cnblogs.com/littlestone08/p/8629184.html
Copyright © 2011-2022 走看看