Registriert seit: 11. Aug 2007
357 Beiträge
|
MacOS ShellExecute
22. Mär 2022, 17:21
Hi,
ich habe in einem älteren Projekt folgendes gemacht um einen Befehl im Terminal auszuführen und mir die Ausgabe in TStrings zu speichern:
Delphi-Quellcode:
type
PIOFile = Pointer;
// Create a new stream connected to a pipe running the given command.
function popen(const Command: MarshaledAString; Modes: MarshaledAString): PIOFile; cdecl;
external libc name _PU + '_popen';
{$EXTERNALSYM popen}
// Close a stream opened by popen and return the status of its child.
function pclose(Stream: PIOFile): Integer; cdecl; external libc name _PU + '_pclose';
{$EXTERNALSYM pclose}
// Return the EOF indicator for STREAM.
function feof(Stream: PIOFile): Integer; cdecl; external libc name _PU + '_feof';
{$EXTERNALSYM feof}
// Read chunks of generic data from STREAM.
function fread(Ptr: Pointer; Size: LongWord; N: LongWord; Stream: PIOFile)
: LongWord; cdecl; external libc name _PU + '_fread';
{$EXTERNALSYM fread}
// Wait for a child to die. When one does, put its status in *STAT_LOC
// and return its process ID. For errors, return (pid_t) -1.
function wait(__stat_loc: PInteger): Integer; external libc name _PU + '_wait';
{$EXTERNALSYM wait}
procedure ExecCmdline(const CmdLine: string; CmdResult: TStrings);
var
Output: PIOFile;
Buffer: PAnsiChar;
TempString: AnsiString;
Line: AnsiString;
BytesRead: Integer;
const
BufferSize: Integer = 1000;
begin
TempString := '';
CmdResult.Clear;
Output := popen(PAnsiChar(AnsiString(CmdLine)), 'r');
GetMem(Buffer, BufferSize);
if Assigned(Output) then
try
while feof(Output) = 0 do
begin
BytesRead := fread(Buffer, 1, BufferSize, Output);
SetLength(TempString, Length(TempString) + BytesRead);
Move(Buffer^, TempString[Length(TempString) - (BytesRead - 1)],
BytesRead);
while Pos(AnsiChar(#10), TempString) > 0 do
begin
Line := Copy(TempString, 1, Pos(AnsiChar(#10), TempString) - 1);
if CmdResult <> nil then
CmdResult.Add(UTF8ToString(Line));
TempString := Copy(TempString, Pos(AnsiChar(#10), TempString) + 1,
Length(TempString));
end;
end;
finally
pclose(Output);
wait(nil);
FreeMem(Buffer, BufferSize);
end;
end;
Das scheint aber in BigSur nicht mehr zu gehen, da die Pipe Befehle nicht mehr in der libc Bibliothek vorhanden sind. Gibt es da eine Alternative?
Peter.
|
|
Zitat
|