[Linux] Как прочитать вывод консольного приложения и вывести его в Memo?
uses
{...,} Posix.Base, Posix.Fcntl;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
public
end;
TStreamHandle = Pointer;
var
Form1: TForm1;
function popen(const command: MarshaledAString; const _type: MarshaledAString): TStreamHandle; cdecl; external libc name _PU + 'popen';
function pclose(filehandle: TStreamHandle): int32; cdecl; external libc name _PU + 'pclose';
function fgets(buffer: Pointer; size: Int32; Stream: TStreamHandle): Pointer; cdecl; external libc name _PU + 'fgets'
implementation
{$R *.fmx}
var
Thread: TThread = nil;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread := TThread.CreateAnonymousThread(
procedure
var
Handle: TStreamHandle;
Data: array[0..511] of UInt8;
str: string;
begin
Handle := popen(MarshaledAString(UTF8STring('ping ya.ru')), 'r');
try
while fgets(@Data[0], SizeOf(Data), Handle) <> nil do
begin
str := TrimRight(UTF8ToString(@Data[0]));
TThread.Synchronize(TThread.Current,
procedure
begin
Memo1.Lines.Add(str);
end
);
if TThread.Current.CheckTerminated then
Break;
end;
finally
pclose(Handle);
end;
FreeAndNil(Thread);
end
);
Thread.FreeOnTerminate := True;
Thread.Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Assigned(Thread) then
if Thread.Started then
begin
Thread.Terminate;
Thread := nil;
end;
end;
|