Capturing all of the Output from a Console application (32 bit) (Views: 100)


Use the function GetDosOutput in your application to capture all the output from a DOS application (this version only supports 32-bit console applications for how to do this with 16 bit see the update to this article "Capturing all of the Output from a Console application (16 bit)").

Code recieved from Mike Lischke (Team JEDI) in response to a question I asked on the borland winapi newsgroup.  It came from his app "Compiler Generator" (www.lischke-online.de/DCG.html) and then was converted to the GetDosOutput function by me.  You can contact me at johnwlong@characterlink.net.


unit consoleoutput;


  Controls, Windows, SysUtils, Forms;

function GetDosOutput(const CommandLine: string): string;


function GetDosOutput(const CommandLine: string): string;
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of Char;
  BytesRead: Cardinal;
  WorkDir, Line: string;
  with SA do
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  // create pipe for standard output redirection
  CreatePipe(StdOutPipeRead, // read handle
    StdOutPipeWrite, // write handle
    @SA, // security attributes
    0 // number of bytes reserved for pipe - 0
    // Make child process use StdOutPipeWrite as standard out,
    // and make sure it does not show on screen.
    with SI do
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect std
        hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;

    // launch the command line compiler
    WorkDir := ExtractFilePath(CommandLine);
    WasOK := CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil,
      PChar(WorkDir), SI, PI);

    // Now that the handle has been inherited, close write to be safe.
    // We don't want to read or write to it accidentally.
    // if process could be created then handle its output
    if not WasOK then
      raise Exception.Create('Could not execute command line!')
      // get all output until dos app finishes
      Line := '';
        // read block of characters (might contain carriage returns and  line feeds)
        WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);

        // has anything been read?
        if BytesRead > 0 then
          // finish buffer to PChar
          Buffer[BytesRead] := #0;
          // combine the buffer with the rest of the last run
          Line := Line + Buffer;
      until not WasOK or (BytesRead = 0);
      // wait for console app to finish (should be already at this point)
      WaitForSingleObject(PI.hProcess, INFINITE);
      // Close all remaining handles
    result := Line;


<< Back to main page