[fpc-pascal] Re: Getting an output string from a TProcess

Marco van de Voort marcov at stack.nl
Sat Jun 2 11:24:47 CEST 2012


In our previous episode, leledumbo said:
> > So I ask: what's the correct code to inform as argument a TProcess and 
> a instruction string and as a return, to get the output string?

If very up to date FPC 2.7.1:

var s : ansistring;

if RunCommand(binaryname,['arg1','arg2'],s) then
  begin
    // success!
  end;

with binary name '/bin/df' or 'c:\windows\notepad.exe'

There are a couple of variants, see below. The variants that are equivalent
to "commandline" property use are deprecated.  (the unit this came from is
older than the cmdline deprecation, and these variants will share whatever
faith .commandline befalls)

The functions are basically minimal TProcess wrappers based on the already
named wiki page.

// most complex, allows separation of execution status and return value of
// product.
function RunCommandIndir(const curdir:string;const exename:string;const
commands:array of string;var outputstring:string;var
exitstatus:integer):integer;

// simpler form. In scripting uses, success or not is the only interesting
// metric.
function RunCommandIndir(const curdir:string;const exename:string;const
commands:array of string;var outputstring:string):boolean;

// simplest form, no dir.
function RunCommand(const exename:string;const commands:array of string;var
outputstring:string):boolean;

// real quick and dirty work. Deprecated?
function RunCommandInDir(const curdir,cmdline:string;var
outputstring:string):boolean; deprecated;
function RunCommand(const cmdline:string;var outputstring:string):boolean;
deprecated;




More information about the fpc-pascal mailing list