# Thread: How to execute commands using Simba & Lape.

1. ## How to execute commands using Simba & Lape.

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:
w5qyEyTb.png

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

Simba Code:
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:
w5qyEyTa.png

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/.../tprocess.html

FPC wiki of TProcess:
http://wiki.freepascal.org/Executing...grams#TProcess

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

3. 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...ighlight=batch
Last edited by Clutch; 02-15-2016 at 04:12 AM.

4. @Clutch;

Gravedig? Search before you bump.

5. Originally Posted by Kasi
@Clutch;

Gravedig? Search before you bump.
Just trying to bump up my post count.

6. Originally Posted by Clutch
Just trying to bump up my post count.
y ?

7. SRL Junior Member
Join Date
Jan 2008
Posts
33
Mentioned
0 Post(s)
Quoted
0 Post(s)
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)
Last edited by penguII; 12-04-2018 at 10:57 AM.