[fpc-pascal]Using Tprocess to write to child process standard input.
Michael Van Canneyt
michael.vancanneyt at wisa.be
Tue Mar 9 15:32:01 CET 2004
On Tue, 9 Mar 2004, Vincent Snijders wrote:
> Hi,
>
> I am having some problems writing to the InputStream of a Tprocess
> object. Below is the source code. I am testing this on windows. When
> stepping through the code I get INVALID_HANDLE as return value of the
> FileWrite in THandleStream.Write.
> I think there is a bug somewhere in the Tprocess code for win32, but
> before digging deeper I would like to know if anybody has succesfully
> used Tprocess like this on win32 platform.
>
> Regards,
> Vincent.
>
> The following procedure is from the parent application.
> procedure DoIt;
> var
> LogProcess: TProcess;
> procedure WriteLine(const s: string);
> var
> buffer: string;
> begin
> buffer := s + LineEnding;
> LogProcess.Input.Write(buffer, Length(buffer));
> end;
Eh ?? This should be
LogProcess.Input.Write(buffer[1], Length(buffer));
Otherwise you will get some VERY strange things.
Secondly, in your logger app, you forget to do init, to open the file.
Michael.
> begin
> LogProcess := TProcess.Create(nil);
> LogProcess.Options := [poUsePipes];
> LogProcess.CommandLine := 'logger.exe';
> LogProcess.Execute;
> WriteLine('test');
> WriteLine('q');
> LogProcess.WaitOnExit;
> end;
>
> The logger application is simply reads line from stdin and writes temp
> to a file, until the line 'q' is received. The source is this:
> program logger;
>
> {$mode objfpc}{$H+}
>
> uses
> Classes;
>
> var
> logfile: text;
> line: string;
>
> procedure init;
> begin
> assignfile(logfile, 'd:\troep\log.txt');
> rewrite(logfile);
> writeln(logfile, 'Started logging');
> closefile(logfile);
> end;
>
> procedure AppendText(line: string);
> begin
> append(logfile);
> writeln(logfile, line);
> closefile(logfile);
> end;
>
> begin
> repeat
> readln(line);
> appendText(line);
> until line='q';
> end.
>
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list