PDA

View Full Version : How to execute commands using Simba & Lape.



CynicRus
04-16-2015, 11:53 AM
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:
25638

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:
25637

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
04-16-2015, 12:55 PM
Should note that CommandLine is deprecated, Should really be using executable & parameters which sadly isn't as simple but isn't too bad. :)

Clutch
02-15-2016, 03:35 AM
This brings up a question on whether or not there is a parameter to automate "play" button in simba so that we could just call on a script.

May sound useless but there's multiple uses here;
You can have a script launch into another script which may begin you in starting another account to start training or whatever - then have that script launch the other script once it's finished to log back into your other account ultimately creating a loop. Obviously would require a flawless script but..
I'm specifically curious for OgLib - as closing simba and starting a new instance helps free up resources and get's rid of the lag that accumulates over time.

Edit: Found what I was looking for https://villavu.com/forum/showthread.php?t=114592&highlight=batch

Kasi
02-15-2016, 10:43 AM
Clutch;

Gravedig? Search before you bump.

Clutch
02-16-2016, 12:58 AM
Clutch;

Gravedig? Search before you bump.

Just trying to bump up my post count.

Kasi
02-16-2016, 01:36 AM
Just trying to bump up my post count.

y ?

penguII
12-03-2018, 06:43 PM
Hello :)

I tried this with the latest simba version but it crashes when I run a command ?
Anyone knows a fix for this ?

//EDIT: Got it already. It still works perfect :) It just crashed because of the command I used (infinite loop in repeat)