[fpc-pascal] Redirecting input to a child process

SteveG steveg at nevets.com.au
Sun May 8 03:42:51 CEST 2011


On 08/05/11 05:38, Anton Shepelev wrote:
> 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;
> type
>        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;  );
>        end;
>
> var
>     MoreProcess: TProcess;
>     readCount:   integer;
>     strBuf:      TStrBuf;
>
> begin
>     MoreProcess := TProcess.Create(nil);
>     MoreProcess.CommandLine := 'C:\WINDOWS\system32\cmd.exe /C more';
>     MoreProcess.Options := [poUsePipes];
>     MoreProcess.Execute;
>     strBuf.txt := 'Anton';
>     MoreProcess.Input.Write(strBuf.buf, strBuf.size);
>     MoreProcess.CloseInput();
>     writeLn('Waiting...');    //This never ends
>     while MoreProcess.Running do begin Sleep(50); end;
>     writeLn('Wait finished.');
>     Sleep(100);
>     strBuf.size := MoreProcess.Output.Read(strBuf.buf, 255);
>     writeLn(strBuf.txt);
> end.
>
> Could you please help me to make it work?
>
> Thanks in advance,
> Anton

Anton - I have attached an extract from some working code (hopefully I 
didnt remove anything necessary)
It may help you move on a bit further

SteveG




-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Process.txt
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20110508/de058f7c/attachment.txt>


More information about the fpc-pascal mailing list