Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-06

Getting an output string from a TProcess

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,

I want to execute FPC, but filtering the output in order to show only the necessary information. The reason is I am blind, I use screen reader and, in the console environment, it's not possible to read the rows below without firstly read the rows above.
My intention is to use TProcess, calling fpc commmandline, replacing all the non necessary information by ''. My first line will by something like "compiling ' or 'linking '.
For this purpose, TProcess is wonderful.
I want a function that receives a commandline string and returns an output string. This return will be afterwards used to replacements.

function execute(AProcess: TProcess; instr: string):string;
var
buffer: integer;
begin
with AProcess do
begin
commandline := instr;
options := [poUsePipes];
execute;
output.read(buffer, 10);
result := buffer;
end;
end;

I am confused due to some reasons:
1. Commandline is signed as deprecated, so what is the best way to pass a string to TProcess? Is the commandline the first item of the tstrings called parameter?
2. The output is a class derived from THandleStream. As the read method is override, its behaviour is different from TStream. 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?

Regards,
Luciano

leledumbo
to luciano de souza
Commandline is signed as deprecated, so what is the best way to

pass a string to TProcess? Is the commandline the first item of the tstrings called parameter?

http://www.freepascal.org/docs-html/fcl/process/tprocess.commandline.html

See something that explains it there?

The output is a class derived from THandleStream. As the read

method is override, its behaviour is different from TStream.

Not really, it still conforms to the Read method of TStream, the implementation should of course be adjusted to read from a process' output stream.

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?

http://wiki.lazarus.freepascal.org/Executing_External_Programs#TProcess

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Getting-an-output-string-from-a-TProcess-tp5709934p5709938.html Sent from the Free Pascal - General mailing list .
Marco van de Voort
to leledumbo

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;
Marco van de Voort
to leledumbo

In our previous episode, leledumbo said:

WUAOOOW!!! Where are those functions, Marco?

In 2.7.1 (trunk)

Which revision implements them?

r21437

And last but not least, do those functions consider BIG output?

Yes. As said in the original message, the base is the large output wiki source.

Please test them, then they can go into 2.6.2 (riskless since they don't modify anything, they only add).