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):

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;


-- 
"80% Of a hardware engineer's job is application of the uncertainty principle.
80% of a software engineer's job is pretending this isn't so."
A.J. Venter
Chief Software Architect
OpenLab International
http://www.getopenlab.com       | +27 82 726 5103 (South Africa)
http://www.silentcoder.co.za    | +55 118 162 2079 (Brazil)

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to