On Tue, 7 Mar 2006 15:48:38 +0200
"A.J. Venter" <[EMAIL PROTECTED]> wrote:
> On Tuesday 07 March 2006 14:49, Sam Washkansky wrote:
> > Thanks a lot I would appreciate that.
> > Cheers
> > Sam
> >
> Here you go, I´m not going to post the whole huge unit now, just the two
> procedures, you can use them as you see fit, they are pretty much just
> adaptations of the examples in the tutorial anyway (note that they are
> overloaded):
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?
Mattias
>
> procedure execute (command:string; var output:Tstrings);
> {Run command and store the output from it in output
> Notes:
> 1) do not create output prior to assigning it
> 2) you must free output yourself
> }
> const
> READ_BYTES = 2048;
>
> Var
> Process :Tprocess;
> MemStream : TMemoryStream;
> n: LongInt;
> BytesRead: LongInt;
> begin
> Process := TProcess.create(nil);;
> Process.CommandLine := command;
>
>
>
> {Actually run the thing and catch the output}
> MemStream := TMemoryStream.Create;
> outPut := TStringList.Create;
> BytesRead := 0;
>
> Process.Options := [poUsePipes,poNoConsole];
>
> Process.Execute;
>
> while Process.Running do
> begin
> // make sure we have room
> MemStream.SetSize(BytesRead + READ_BYTES);
>
> // try reading it
> n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
> if n > 0
> then begin
> Inc(BytesRead, n);
> end
> else begin
> // no data, wait 100 ms
> Sleep(100);
> end;
> end;
> // read last part
> repeat
> // make sure we have room
> MemStream.SetSize(BytesRead + READ_BYTES);
> // try reading it
> n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
> if n > 0
> then begin
> Inc(BytesRead, n);
> end;
> until n <= 0;
> MemStream.SetSize(BytesRead);
> OutPut.LoadFromStream(MemStream);
> MemStream.Free;
> Process.Free;
> end;
>
> procedure execute (command:string);
> {Execute command, do not wait for it to exit, do
> not capture output.}
>
> Var
> Process :Tprocess;
> begin
> Process := TProcess.create(nil);;
> Process.CommandLine := command;
> Process.Options := [poNoConsole];
> Process.Execute;
> Process.Free;
> end;
>
>
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives