SRL Archive - OSR Advanced Scripting Tutorials

How to execute commands using Simba & Lape.


CynicRus

2015-04-16 07:53:38

Hey guys. Many users do not know about the class TProcess. That class allows you to launch an external application in Lazarus. And, it also exported to Lape. Today I'll show you how to use it. How does this help you? You are only limited by your imagination.

So, my test form looks:
[ATTACH=CONFIG]25638[/ATTACH]

And then we use the functional of TProcess to execute an external program and get it output:


procedure RunCommand(Cmd: string; Lines: TStrings);
var
AProcess: TProcess;
Buf: array [1..BUFSIZE] of char;
I, Count: longint;
ss: string;
begin
Lines.Clear;
AProcess.Init(nil);
try
AProcess.SetCommandline(Cmd);
AProcess.setOptions([poUsePipes, poNoConsole]);
AProcess.execute;
repeat
count := AProcess.GetOutput().Read(buf, BufSize);
for I := 1 to count do
ss := ss + buf[i];
until Count = 0;
Lines.Add(ss);
finally
AProcess.Free;
end;
end;
After call of this function with params like 'netstat -an' we get the result:
[ATTACH=CONFIG]25637[/ATTACH]

Well, after that, as I said - you can only limit is your imagination. I showed you the mechanism by which it is possible to significantly increase the scope of the Simba using.
My test script can be found in attachment.

Thank you for attention!

Cheers,
Cynic.

PS:

Reference of TProcess methods:
http://www.freepascal.org/docs-html/fcl/process/tprocess.html

FPC wiki of TProcess:
http://wiki.freepascal.org/Executing_External_Programs#TProcess

Olly

2015-04-16 08:55:29

Should note that CommandLine is deprecated, Should really be using executable & parameters which sadly isn't as simple but isn't too bad. :)