首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Shellexecute等价物用于Linux作为目标平台

Shellexecute等价物用于Linux作为目标平台
EN

Stack Overflow用户
提问于 2018-02-04 22:58:02
回答 2查看 657关注 0票数 2

有没有人知道作为主动目标平台的Linux的Shellexecute命令的等价物?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-25 13:55:07

结果是这样的:

代码语言:javascript
运行
复制
const
   libc = '/usr/lib/libc.dylib';

type
   PIOFile = Pointer;

   // Create a new stream connected to a pipe running the given command.
function popen(const Command: PAnsiChar; Modes: PAnsiChar): PIOFile; cdecl; external libc name 'popen';

// Close a stream opened by popen and return the status of its child.
function pclose(Stream: PIOFile): Integer; cdecl; external libc name 'pclose';

// Return the EOF indicator for STREAM.
function feof(Stream: PIOFile): Integer; cdecl; external libc name 'feof';

// Read chunks of generic data from STREAM.
function fread(Ptr: Pointer; Size: LongWord; N: LongWord; Stream: PIOFile): LongWord; cdecl; external libc name '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; cdecl; external libc name 'wait';


procedure TUtils.RunCommand(const CmdLine: string; results: TStrings);
var
   Output: PIOFile;
   Buffer: PAnsiChar;
   TempString: Ansistring;
   Line: Ansistring;
   BytesRead: Integer;
const
   BufferSize: Integer = 1000;
begin
   TempString := '';
   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(#10, TempString) > 0 do
            begin
               Line := Copy(TempString, 1, Pos(#10, TempString) - 1);
               results.Add(UTF8ToString(Line));

               TempString := Copy(TempString, Pos(#10, TempString) + 1, Length(TempString));
            end;
         end;
      finally
         pclose(Output);
         wait(nil);
       FreeMem(Buffer, BufferSize);
      end;
end;
票数 0
EN

Stack Overflow用户

发布于 2018-02-05 18:25:18

代码语言:javascript
运行
复制
procedure ShlOpen( FileName: String ) ;   
var prc: TProcess;   
begin   
 prc: = TProcess.Create ( nil ) ;  
 prc.CommandLine: = 'xdg-open' + FileName;  
 prc.Execute;  
 prc.free;  
end ;  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48609416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档