[fpc-pascal] Redirecting input to a child process

Anton Shepelev anton.txt at gmail.com
Sat May 7 22:08:46 CEST 2011

Hello all,

I  have been experimenting with redirection of stan-
dard I/O of child processes using  FreePascal  2.4.2
on  Windows.  I have succeeded in capturing standard
input and standard output, but failed to feed my own
data to the child's standard input.

The  child process doesn't seem to receive any input
at all, staying blocked forever.

At first I thought it was a problem with my usage of
WinApi,  so  I rewrote my program using the TProcess
class... just to get the same  result.  The  program
feeds  'Anton'  to the more.com program and captures
part of its output:

Program StrRedir;
uses Classes, Process, Sysutils;
const MaxByte = 255;
      TStrBuf = packed record {As a way to read buffers into strings}
      case Boolean of
         true: (  size: Byte;
                  buf:  array[0..MaxByte] of Char;
         false:(  txt:  ShortString;  );

   MoreProcess: TProcess;
   readCount:   integer;
   strBuf:      TStrBuf;

   MoreProcess := TProcess.Create(nil);
   MoreProcess.CommandLine := 'C:\WINDOWS\system32\cmd.exe /C more';
   MoreProcess.Options := [poUsePipes];
   strBuf.txt := 'Anton';
   MoreProcess.Input.Write(strBuf.buf, strBuf.size);
   writeLn('Waiting...');    //This never ends
   while MoreProcess.Running do begin Sleep(50); end;
   writeLn('Wait finished.');
   strBuf.size := MoreProcess.Output.Read(strBuf.buf, 255);

Could you please help me to make it work?

Thanks in advance,

More information about the fpc-pascal mailing list