Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-03

Using Tprocess to write to child process standard input.

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders

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;
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.

Reply by email

Michael Van Canneyt
to Vincent Snijders
Vincent Snijders wrote 24 lines
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.

Show quoted text 49 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Vincent Snijders
to Michael Van Canneyt

Michael,

Thanks for your comments.

Show quoted text 13 lines
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.

<source snipped>

Show quoted text 6 lines
Eh ?? This should be LogProcess.Input.Write(buffer[1], Length(buffer)); Otherwise you will get some VERY strange things.

Thanks for the hint. I made the changes, but it doesn't solve the problem though.

Secondly, in your logger app, you forget to do init, to open the file.

Sorry, this was a cut and past error.

To prevent this I add the source in a zip.

If I do the following in dos-box, a log.txt is created: type q.txt | logger.exe
So I conclude logger is working as expected.

but
running testprocess.exe
doesn't return, and only the header of the log file is written, so the logger hasn't received any info from testprocess.exe.

Vincent.

Reply by email

Unknown author
to Vincent Snijders
Vincent Snijders wrote 45 lines
Michael, Thanks for your comments. On Tue, 9 Mar 2004 15:32:01 +0100 (W. Europe Standard Time) Michael Van Canneyt wrote: > > > 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. <source snipped> > > Eh ?? This should be > > LogProcess.Input.Write(buffer[1], Length(buffer)); > > Otherwise you will get some VERY strange things. Thanks for the hint. I made the changes, but it doesn't solve the problem though. > > Secondly, in your logger app, you forget to do init, to open the file. Sorry, this was a cut and past error. To prevent this I add the source in a zip. If I do the following in dos-box, a log.txt is created: type q.txt | logger.exe So I conclude logger is working as expected. but running testprocess.exe doesn't return, and only the header of the log file is written, so the logger hasn't received any info from testprocess.exe.

I will test tomorrow at work (don't have winblows currently here at home)

Michael.

Reply by email

Michael Van Canneyt
to Vincent Snijders: Thanks for your comments. On Tue, 9 Mar 2004 15:32:01 +0100 (W. Europe Standard Time)…
Vincent Snijders wrote 40 lines
Michael, Thanks for your comments. On Tue, 9 Mar 2004 15:32:01 +0100 (W. Europe Standard Time) Michael Van Canneyt wrote: > > > 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. <source snipped> > > Eh ?? This should be > > LogProcess.Input.Write(buffer[1], Length(buffer)); > > Otherwise you will get some VERY strange things. Thanks for the hint. I made the changes, but it doesn't solve the problem though. > > Secondly, in your logger app, you forget to do init, to open the file. Sorry, this was a cut and past error. To prevent this I add the source in a zip. If I do the following in dos-box, a log.txt is created: type q.txt | logger.exe So I conclude logger is working as expected.

That is what I also thought, until I found out that
- The log file didn't contain the contents of q.txt
- I was actually executing the cygwin logger program.

Show quoted text 5 lines
but running testprocess.exe doesn't return, and only the header of the log file is written, so the logger hasn't received any info from testprocess.exe.

It works fine here if I change commandline to '.\logger.exe'

Michael.

Reply by email

Vincent Snijders
to Michael Van Canneyt
Show quoted text 30 lines
On Tue, 9 Mar 2004, Vincent Snijders wrote: > Michael, > > Thanks for your comments. > > On Tue, 9 Mar 2004 15:32:01 +0100 (W. Europe Standard Time) > Michael Van Canneyt wrote: > > > > > > To prevent this I add the source in a zip. > > If I do the following in dos-box, a log.txt is created: > type q.txt | logger.exe > So I conclude logger is working as expected. That is what I also thought, until I found out that - The log file didn't contain the contents of q.txt - I was actually executing the cygwin logger program. > > but > running testprocess.exe > doesn't return, and only the header of the log file is written, so the > logger hasn't received any info from testprocess.exe. It works fine here if I change commandline to '.\logger.exe'

I have been trying to find out, why it works on your computer and not on mine.

It turns out, that it fails with fpc 1.0.11 (5-3-2004) and works with fpc 1.9.3 (10-3-2004). Windows and Linux versions behave the same way (failure with fpc 1.0.11 and success with fpc 1.9.3).

In both versions I am using process.pp version 1.19.

Do you have a clue, what might cause these different behaviour?

Compiling with 1.9.3 is not a option at the moment, because I need to be able to use a debugger and debug info is still not working right.

TIA,
Vincent.

Reply by email