1.Lazarus执行外部程序之RunCommand
在 Lazarus 中, 关于如何执行一个外部 command(命令)或 process(进程)或 program(程序),可以使用如下表中的方法:
方法
Library
Platforms
单行代码
功能
ExecuteProcess
RTL
跨平台
是
非常有限
ShellExecute
WinAPI
仅微软Windows系统
是
较多
fpsystem, fpexecve
Unix
仅Unix系统
TProcess
FCL
跨平台
否
完全
RunCommand
FCL
跨平台 需要FPC 2.6.2+
是
涉及共有的TProcess用法
OpenDocument
LCL
跨平台
是
仅打开文档。
在实际开发中,我们比较常用的是:ShellExecute、TProcess、RunCommand 和 OpenDocument 4 个方法,本节我们来学习 RunCommand,是可以跨平台的,在 Process 单元中。
1.1 RunCommand
RunCommand 在当前工作目录执行命令, 使用 CmdLine 的版本尝试将命令行拆分为二进制和单独的命令行参数。此版本的函数已弃用。
在 Process 单元中的 RunCommand 函数原型:
function RunCommand( const exename: TProcessString; const commands: array of TProcessString; out outputstring: string; Options: TProcessOptions = []; SWOptions: TShowWindowOptions = swoNone):Boolean;
参数:
- exename - 程序名
- commands - 命令行参数
- outputstring - 进程输出的字符串
- Options - 进程选项
- SWOptions - 显示进程的窗口选项。默认值为 swoNone
function RunCommand( const cmdline: TProcessString; out outputstring: string):Boolean;
参数:
cmdline - 要启动的二进制文件的限定路径,以及由空格分隔的命令行参数
outputstring - 进程输出的字符串
函数结果:
如果函数执行成功则为 True,否则返回 False。
1.2 RunCommandIndir
RunCommandInDir 将使用命令行选项 commands 执行二进制 exename,将 curdir设置为命令的当前工作目录。 捕获命令的输出,并在字符串 OutputString 中返回。该函数等待命令完成,如果命令启动成功,则返回 True ,否则返回 False。在返回值为整数的情况下,成功为零,错误为-1。如果指定了 ExitStatus 参数,则在此参数中返回命令的退出状态。
使用 cmdline 的版本尝试将命令行拆分为二进制和单独的命令行参数,此版本的函数已弃用。
在 Process 单元中的 RunCommandIndir 函数原型:
function RunCommandIndir( const curdir: TProcessString; const exename: TProcessString; const commands: array of TProcessString; out outputstring: string; out exitstatus: Integer; Options: TProcessOptions = []; SWOptions: TShowWindowOptions = swoNone):Integer;
参数:
- curdir - 命令的当前工作目录
- exename - 可执行程序
- commands - 可执行文件的命令行参数
- outputstring - 命令输出的字符串
- exitstatus - 退出时进程的退出状态
- Options - 运行命令时使用的选项
- SWOptions - 示进程的窗口选项,默认值为 swoNone
function RunCommandIndir( const curdir: TProcessString; const exename: TProcessString; const commands: array of TProcessString; out outputstring: string; Options: TProcessOptions = []; SWOptions: TShowWindowOptions = swoNone):Boolean;
参数:
- curdir - 命令的当前工作目录
- exename - 可执行程序
- commands - 可执行文件的命令行参数
- outputstring - 命令输出的字符串
- Options - 运行命令时使用的选项
- SWOptions - 示进程的窗口选项,默认值为 swoNone
function RunCommandInDir( const curdir: TProcessString; const cmdline: TProcessString; out outputstring: string):Boolean;
参数:
- curdir - 命令的当前工作目录
- cmdline - 要执行的二进制文件名加上由空格分隔的命令行参数
- outputstring - 命令输出的字符串
函数结果:
如果命令成功启动,则为真(或在整数返回值的情况下为零)。
错误:
出错时,返回False。
1.3 Options
启动进程时使用的选项:
- poRunSuspended - 以挂起状态启动进程
- poWaitOnExit - 等待进程终止后再返回
- poUsePipes - 使用管道重定向标准输入和输出
- postderrToOutPut - 将标准错误重定向到标准输出流
- poNoConsole - 不允许访问进程的控制台窗口(仅限 Win32)
- poNewConsole - 为进程启动一个新的控制台窗口(仅限 Win32)
- poDefaultErrorMode - 使用默认错误处理
- poNewProcessGroup - 在新进程组中启动进程(仅限 Win32)
- poDebugProcess - 允许调试进程(仅限 Win32)
- poDebugOnlyThisProcess - 不遵循此进程启动的进程(仅限 Win32)
- poDetached - 在 Windows 上使用 DETACHED_PROCESS 创建标志运行进程
- poPassInput - 将标准输入句柄传递给新进程
- poRunIdle - 向事件处理程序发出信号以等待进程的运行循环中的输出。
1.4 示例
在窗体中添加两个按钮和一个 Memo 组件,单击第一个按钮在当前目录下执行 dir /s /a,单击第二个按钮在指定目录下执行 dir /s /a。代码如下:
procedure TForm1.Button1Click(Sender: TObject);var OutputString: String;begin if RunCommand('cmd', ['/c', 'dir', '/s', '/a'], OutputString) then Memo1.Lines.Add(OutputString);end;procedure TForm1.Button2Click(Sender: TObject);var OutputString: String;begin if RunCommandIndir('E:published', 'cmd', ['/c', 'dir', '/s', '/a'], OutputString) then Memo1.Lines.Add(OutputString);end;