[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

How to Redirect stdout/stderr from a spawned process to application?

Asked by swift99 in Delphi Programming

Tags: stdout, redirect

I'm trying to encapsulate a call to create a shell process and capture its stdout/stderr from named pipes for further processing.  

I can capture the stdout from a separately started process, but not one spawned from within the application.  I suspect (very strongly) that I am missing a flag or setting for CreateProcess that would allow this.  That is, if I start the process from a command line with "ipconfig > \\.\pipe\{whatever-the-id-is}" I get the stdout when I read the stream.  However, if I start the process with CreateProcess I do not.

Here's the encapsulation code:

unit ShellEncapsulation;

interface

uses Classes, Sysutils, Windows, ShellAPI;

type
  TShell = class (TObject)
  private
    FExecResults: Boolean;
    FStdErrName: String;
    FCommandLine: String;
    FStdOutName: String;
    FStderr: TStream;
    FStdout: TStream;
    hStdErr: THandle;
    hStdOut: THandle;
    Fbuffersize: Integer;
    FTimeOut: Integer;

    procedure SetCommandLine(const Value: String);
    procedure SetExecResults(const Value: Boolean);
    procedure SetStderr(const Value: TStream);
    procedure SetStdErrName(const Value: String);
    procedure SetStdout(const Value: TStream);
    procedure SetStdOutName(const Value: String);
    procedure Setbuffersize(const Value: Integer);
    procedure SetTimeOut(const Value: Integer);
  protected
    function GetUniqueName: String;

    property CommandLine: String read FCommandLine write SetCommandLine;
    property StdErrName: String read FStdErrName write SetStdErrName;
    property StdOutName: String read FStdOutName write SetStdOutName;
  public
    Constructor Create;
    Destructor Done;

    function Exec (_Commandline: String): Boolean;

    property Stdout: TStream read FStdout write SetStdout;
    property Stderr: TStream read FStderr write SetStderr;
    property ExecResults: Boolean read FExecResults write SetExecResults;
    property buffersize: Integer read Fbuffersize write Setbuffersize;
    property TimeOut: Integer read FTimeOut write SetTimeOut;
  end;

implementation

{ TShell }

constructor TShell.Create;
begin
  buffersize := 1024 * 1024;  // Allow 1MB Buffer
  Timeout := 10000;           // Allow 10 seconds time out
end;

destructor TShell.Done;
begin
//  StdOut.Close;
  StdOut.Free;
  StdOut := NIL;
  DisconnectNamedPipe (hStdout);
//  DeleteFile (StdOutName);


//  StdErr.Close
  StdErr.Free;
  StdErr := Nil;
  DisconnectNamedPipe (hStderr);
//  DeleteFile (StdErrName);
end;

function TShell.Exec(_Commandline: String): Boolean;
var
  StartUpInfo : TStartUpInfo;
  ProcessInfo : TProcessInformation;
  cmdLine: String;
begin
  stdoutname := GetUniquename;
  stderrname := GetUniquename;

  hStdOut := CreateNamedPipe (PChar(stdOutName),
    PIPE_ACCESS_INBOUND,
    PIPE_TYPE_BYTE+PIPE_WAIT,
    99,
    bufferSize,
    bufferSize,
    TimeOut,
    NIL);

  hStdErr := CreateNamedPipe (PChar(stdErrName),
    PIPE_ACCESS_INBOUND,
    PIPE_TYPE_BYTE+PIPE_WAIT,
    99,
    bufferSize,
    bufferSize,
    TimeOut,
    NIL);

  StdOut := THandleStream.Create (hStdOut);
  StdErr := THandleStream.Create (hStdErr);

  FillChar(StartUpInfo, SizeOf(TStartUpInfo), 0);
  with StartUpInfo do begin
    cb          := SizeOf(TStartUpInfo);
    dwFlags     := STARTF_USESTDHANDLES;
    wShowWindow := SW_SHOWDEFAULT;
    hStdOutput  := hStdOut;
    hStdError   := hStdErr;
  end; // with

  ExecResults := CreateProcess(nil,
                PChar(_CommandLine),
                nil,
                nil,
                false,
                DETACHED_PROCESS + NORMAL_PRIORITY_CLASS,
                nil,
                PChar(ExtractFilePath(paramStr(0))),
                StartUpInfo,
                ProcessInfo);
  Result := ExecResults;
end;

function TShell.GetUniqueName: String;
var
  aGUID: TGUID;
begin
  CreateGUID (aGUID);
//  result := '\\.\PIPE\'+Copy (GUIDToString (aGUID),2,8);
  result := '\\.\PIPE\'+GUIDToString (aGUID);
end;

procedure TShell.Setbuffersize(const Value: Integer);
begin
  Fbuffersize := Value;
end;

procedure TShell.SetCommandLine(const Value: String);
begin
  FCommandLine := Value;
end;

procedure TShell.SetExecResults(const Value: Boolean);
begin
  FExecResults := Value;
end;

procedure TShell.SetStderr(const Value: TStream);
begin
  FStderr := Value;
end;

procedure TShell.SetStdErrName(const Value: String);
begin
  FStdErrName := Value;
end;

procedure TShell.SetStdout(const Value: TStream);
begin
  FStdout := Value;
end;

procedure TShell.SetStdOutName(const Value: String);
begin
  FStdOutName := Value;
end;

procedure TShell.SetTimeOut(const Value: Integer);
begin
  FTimeOut := Value;
end;

end.
 
Related Solutions
Keywords: How to Redirect stdout/stderr from a …
 
Loading Advertisement...
 
[+][-]07/18/03 03:41 AM, ID: 8950631Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Delphi Programming
Tags: stdout, redirect
Sign Up Now!
Solution Provided By: DragonSlayer
Participating Experts: 1
Solution Grade: A
 
[+][-]07/18/03 06:03 AM, ID: 8951377Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/18/03 07:21 AM, ID: 8952049Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92