[Lazarus] Can't copy TMemoryStream to TProcess.Input

Mattias Gaertner nc-gaertnma at netcologne.de
Sat Apr 6 00:04:07 CEST 2013


On Fri, 5 Apr 2013 17:33:22 -0300
Leonardo M. Ramé <l.rame at griensu.com> wrote:

> Hi, it must be that I've been working for more than 8 hrs but I can't find where the problem is...
> 
> I need to pass a file via stdIn to TProcess, then capture the StdOut. This
> program should do what I expect, but, instead it hangs after writing aprox
> 293000 bytes to Proc.Input stream.
> 
> Can you take a look at it?. 
> 
> program test;
> 
> {$mode objfpc}{$H+}
> 
> uses
>   SysUtils, Classes, 
>   process,
>   math;
> 
> 
> function CompressLZMA(AStream: TMemoryStream): TMemoryStream;
> var
>   Proc: TProcess;
>   lBuffer: array [0..2048] of byte;
>   ReadCount: integer;
>   lPos: LongInt;
> begin
>   Result := TMemoryStream.Create;
>   AStream.Position:= 0;
>   writeln('-1-');
>   Proc := TProcess.Create(nil); //Create a new process
>   try
>     Proc.Executable := '/usr/bin/lzma'; //Run lzma
>     Proc.Parameters.Add('--compress');
>     Proc.Options := [poUsePipes, poStderrToOutPut];
>     Proc.Execute; 
> 
>     writeln(format('Writing %d bytes...', [AStream.Size]));
> 
>     lPos := 0;
>     while(lPos < AStream.Size) do
>     begin
>       writeln(format('Pos: %d - Size: %d', [lPos, AStream.Size]));
>       ReadCount := AStream.Read(lBuffer, SizeOf(lBuffer));
>       Proc.Input.Write(lBuffer, ReadCount);
>       lPos := lPos + ReadCount;

After every write to Input, read all available from Output and StdErr.

Mattias




More information about the Lazarus mailing list