At one point I used this function:
procedure ExecCommand(const command : string;
stdout : TStringList;
stderr : TStringList;
const timeoutSec : integer;
out exitStatus : integer);
var
P: TProcess;
nOut : LongInt;
nErr : LongInt;
StdOutput : TMemoryStream;
StdOutBytesRead: LongInt;
StdError : TMemoryStream;
StdErrBytesRead: LongInt;
startTime : TDateTime;
begin
exitStatus := -1;
StdOutput := TMemoryStream.Create;
StdError := TMemoryStream.Create;
StdOutBytesRead := 0;
StdErrBytesRead := 0;
try
P := TProcess.Create(nil);
try
P.CommandLine := command;
P.Options := [poUsePipes];
startTime := now;
P.Execute;
{ read stderr and stdout while the process runs }
while P.Running do begin
StdOutput.SetSize(StdOutBytesRead + READ_BYTES);
StdError.SetSize(StdErrBytesRead + READ_BYTES);
nOut := P.Output.Read((StdOutput.Memory + StdOutBytesRead)^,
READ_BYTES);
if nOut > 0 then begin
Inc(StdOutBytesRead, nOut);
end;
nErr := P.Stderr.Read((StdError.Memory + StdErrBytesRead)^, READ_BYTES);
if nErr > 0 then begin
Inc(StdErrBytesRead, nErr);
end;
if (nErr = 0) and (nOut = 0) then Sleep(100);
if (timeoutSec > 0) and (SecondsBetween(now, startTime) >
timeoutSec) then begin
P.Terminate(exitStatus);
raise Exception.Create('Execution timed out');
end;
end;
exitStatus := P.ExitStatus;
{ read what's left over in stdout }
repeat
StdOutput.SetSize(StdOutBytesRead + READ_BYTES);
nOut := P.Output.Read((StdOutput.Memory + StdOutBytesRead)^,
READ_BYTES);
if nOut > 0 then begin
Inc(StdOutBytesRead, nOut);
end;
until nOut <= 0;
{ read what's left over in stderr }
repeat
StdError.SetSize(StdErrBytesRead + READ_BYTES);
nErr := P.Stderr.Read((StdError.Memory + StdErrBytesRead)^, READ_BYTES);
if nErr > 0 then begin
Inc(StdErrBytesRead, nErr);
end;
until nErr <= 0;
StdOutput.SetSize(StdOutBytesRead);
if Assigned(stdout) then begin
stdout.LoadFromStream(StdOutput);
end;
StdError.SetSize(StdErrBytesRead);
if Assigned(stderr) then begin
stderr.LoadFromStream(StdError);
end;
finally
FreeAndNil(StdOutput);
FreeAndNil(StdError);
FreeAndNil(P);
end;
except
on E : Exception do begin
exitStatus := -1;
if Assigned(stderr) then begin
stderr.Add(E.Message);
end;
end;
end;
end;
-SG
========================
Computer over. Virus = very yes.
Seth Grover
sethdgrover[at]gmail[dot]com
http://grovergamut.blogspot.com
2008/10/8 wile64 :
Show quoted text 55 lines
2008/10/8 wile64
>
>
> 2008/10/8 Vincent Snijders
>>
>> wile64 schreef:
>> > Hi all,
>> >
>> > Je voudrais lancer un programme en ligne de commande (avec Tprocess ps
>> > de probl?me).
>> >
>> > I would run an progam in command line (with Tprocess no problem).
>> >
>> > 1 - But with the recovery of messages for viewing in a TMemo.
>> > 2 - Do not block my application when read the data (the example of a
>> > wiki is awaiting data and read after)
>> > 3 - Send orders. (I think a TInput.write but it does not).
>> >
>> > Is it possible ?
>> >
>> >
>>
>> Yes, look at the last example in the wiki:
>>
>>
http://wiki.lazarus.freepascal.org/Executing_External_Programs#Using_input_and_output_of_a_TProcess
>>
>
> Thanks Vincent,
>
> This link dead this morning !
This work, but not with the program launched :-(
It's posible an program (C++) not use stdin and stdout for create console ?
Thanks
--
Laurent.
"If debugging is the art of removing bugs, then programming must be the art
of creating!"
My Components:
http://wiki.lazarus.freepascal.org/Wile64
French Forum :
http://lazforum-fr.tuxfamily.org/index.php
_______________________________________________
Lazarus mailing list
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus