10

How do I read a console apps output as it is running. I start the console application and would like to read the output as it is printed by the console app.

mghie
  • 32,028
  • 6
  • 87
  • 129

4 Answers4

14

What about this solution.


EDIT: the link leads to this solution (lightly refactored for legibility and to remove use of with):

// The example runs 'chkdsk.exe c:\' and displays the output to Memo1.
// Put a TMemo (Memo1) and a TButton (Button1) on your form. Put this 
//   code in the OnCLick event procedure for Button1:

procedure TForm1.RunDosInMemo(DosApp:String;AMemo:TMemo) ;
const
  ReadBuffer = 2400;
var
  Security    : TSecurityAttributes;
  ReadPipe,
  WritePipe   : THandle;
  start       : TStartUpInfo;
  ProcessInfo : TProcessInformation;
  Buffer      : Pchar;
  BytesRead   : DWord;
  Apprunning  : DWord;

begin
  Security.nlength := SizeOf(TSecurityAttributes) ;
  Security.binherithandle := true;
  Security.lpsecuritydescriptor := nil;
  if Createpipe (ReadPipe, WritePipe, @Security, 0) then 
  begin
    Buffer := AllocMem(ReadBuffer + 1) ;
    FillChar(Start,Sizeof(Start),#0) ;
    start.cb := SizeOf(start) ;
    start.hStdOutput := WritePipe;
    start.hStdInput := ReadPipe;
    start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    start.wShowWindow := SW_HIDE;
    if CreateProcess(nil,
                     PChar(DosApp),
                     @Security,
                     @Security,
                     true,
                     NORMAL_PRIORITY_CLASS,
                     nil,
                     nil,
                     start,
                     ProcessInfo) then
    begin
      repeat
        Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);
        Application.ProcessMessages;
      until (Apprunning <> WAIT_TIMEOUT) ;
      repeat
        BytesRead := 0;
        ReadFile(ReadPipe,Buffer[0],
        ReadBuffer,BytesRead,nil) ;
        Buffer[BytesRead]:= #0;
        OemToAnsi(Buffer,Buffer) ;
        AMemo.Text := AMemo.text + String(Buffer) ;
      until (BytesRead < ReadBuffer) ;
    end;
    FreeMem(Buffer) ;
    CloseHandle(ProcessInfo.hProcess) ;
    CloseHandle(ProcessInfo.hThread) ;
    CloseHandle(ReadPipe) ;
    CloseHandle(WritePipe) ;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
begin
  RunDosInMemo('chkdsk.exe c:\', Memo1) ;
end;
Argalatyr
  • 4,639
  • 3
  • 36
  • 62
ErvinS
  • 1,116
  • 7
  • 13
  • 3
    nice suggestion for those who follow the link, but a link alone is not a really good answer, see: http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Argalatyr Jul 31 '09 at 16:17
  • @Mick: oops - while you were doing that in a new answer, I was editing the old one. If someone feels my edit should be rolled back, feel free to do so. – Argalatyr Jul 31 '09 at 16:34
  • No worries. I deleted my comment then. – Mick Jul 31 '09 at 18:27
  • 1
    This does not read the output as it comes, it only reads everything right at the end ... that is not what i am looking for. –  Aug 03 '09 at 12:56
  • Shame it's not quite what you're looking for, but this code is what I was looking for, thanks none the less. – Robo Mar 24 '10 at 22:25
2

I usually use this ported FPC code: http://www.stack.nl/~marcov/processdelphi.zip

It contains a class to control external programs (it is the class used by Lazarus to call the cmdline compiler and other programs).

Documentation is here, but the delphi port is a bit old, so not all documented properties might exist in the above version.

http://www.freepascal.org/docs-html/fcl/process/index.html

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
1

Just a small addition to Marco answer, using the TProcess unit is thoroughly explained here

I believe this is the easiest way to do it. Good luck!

Faisal Azahrani
  • 123
  • 1
  • 9
0

Just to add few more options.

  1. Two articles building this functionality on top of Windows API: http://thundaxsoftware.blogspot.com/2012/12/capturing-console-output-with-delphi.html

  2. JediVCL library (and it is big, so both having and avoiding it have cons and pros) has a TJvCreateProcess component.
    See https://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvCreateProcess
    And on the installation: https://github.com/project-jedi/jvcl and maybe How to install JVCL packages if the installer failed when I ran it as a non-Administrator User

  3. JediVCL installer uses a small self-contained unit CapExec.pas with a simplistic interface. https://github.com/project-jedi/jvcl/tree/master/jvcl/install/JVCLInstall/Helpers

type
  TCaptureLine = procedure(const Line: string; var Aborted: Boolean) of object;
.....
function CaptureExecute(const App, Args, Dir: string; CaptureLine: TCaptureLine;
  OnIdle: TNotifyEvent = nil;......): Integer;
Arioch 'The
  • 15,799
  • 35
  • 62