【问题标题】:How to get the dos output. In delphi2009 with vista如何获得dos输出。在delphi2009与vista
【发布时间】:2010-06-04 03:11:23
【问题描述】:

我过去常常使用 delphi 获取 dos 输出。
是什么导致http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm 中的代码无法在 vista 上与 delphi2009 一起使用?但它适用于XP中的D7。我不知道要修改哪个部分才能使其工作。

【问题讨论】:

    标签: delphi


    【解决方案1】:

    DelphiDabbler has a solution,虽然我没有亲自测试过:

    function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
    var
      SA: TSecurityAttributes;
      SI: TStartupInfo;
      PI: TProcessInformation;
      StdOutPipeRead, StdOutPipeWrite: THandle;
      WasOK: Boolean;
      Buffer: array[0..255] of AnsiChar;
      BytesRead: Cardinal;
      WorkDir: string;
      Handle: Boolean;
    begin
      Result := '';
      with SA do begin
        nLength := SizeOf(SA);
        bInheritHandle := True;
        lpSecurityDescriptor := nil;
      end;
      CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
      try
        with SI do
        begin
          FillChar(SI, SizeOf(SI), 0);
          cb := SizeOf(SI);
          dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
          wShowWindow := SW_HIDE;
          hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
          hStdOutput := StdOutPipeWrite;
          hStdError := StdOutPipeWrite;
        end;
        WorkDir := Work;
        Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                                nil, nil, True, 0, nil,
                                PChar(WorkDir), SI, PI);
        CloseHandle(StdOutPipeWrite);
        if Handle then
          try
            repeat
              WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
              if BytesRead > 0 then
              begin
                Buffer[BytesRead] := #0;
                Result := Result + Buffer;
              end;
            until not WasOK or (BytesRead = 0);
            WaitForSingleObject(PI.hProcess, INFINITE);
          finally
            CloseHandle(PI.hThread);
            CloseHandle(PI.hProcess);
          end;
      finally
        CloseHandle(StdOutPipeRead);
      end;
    end;
    

    【讨论】:

      【解决方案2】:

      Torry 上也有一个解决方案: http://www.swissdelphicenter.ch/torry/showcode.php?id=683

      【讨论】:

        猜你喜欢
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        • 2017-10-18
        • 2011-05-16
        • 1970-01-01
        相关资源
        最近更新 更多