Discussion:
Executing another application from within a lazarus app
Sam Washkansky
2006-03-07 12:27:41 UTC
Permalink
Hi guys,

I'm new to lazarus and have a quick question. How do you launch an app from
within A lazarus Application. In delphi you could use winexec, or the
ExecuteFile method(in The FMXUtils unit). I would lie this to be platform
independent. I would appreciate any help.

Regards

--
Sam Washkansky
Tel:+27+823738257
email:***@gmail.com
Michael Van Canneyt
2006-03-07 12:34:45 UTC
Permalink
Post by Sam Washkansky
Hi guys,
I'm new to lazarus and have a quick question. How do you launch an app from
within A lazarus Application. In delphi you could use winexec, or the
ExecuteFile method(in The FMXUtils unit). I would lie this to be platform
independent. I would appreciate any help.
The TProcess component is included by default on the component palette.
You can use it to execute and manipulate other programs.

Michael.
Vincent Snijders
2006-03-07 12:35:10 UTC
Permalink
Post by Sam Washkansky
Hi guys,
I'm new to lazarus and have a quick question. How do you launch an app
from within A lazarus Application. In delphi you could use winexec, or
the ExecuteFile method(in The FMXUtils unit). I would lie this to be
platform independent. I would appreciate any help.
See http://wiki.lazarus.freepascal.org/index.php/Executing_External_Programs

Vincent
Micha Nelissen
2006-03-07 12:35:00 UTC
Permalink
Post by Sam Washkansky
Hi guys,
I'm new to lazarus and have a quick question. How do you launch an app
from within A lazarus Application. In delphi you could use winexec, or
the ExecuteFile method(in The FMXUtils unit). I would lie this to be
platform independent. I would appreciate any help.
See:

http://wiki.lazarus.freepascal.org/index.php/Executing_External_Programs

Micha
A.J. Venter
2006-03-07 12:46:34 UTC
Permalink
Post by Sam Washkansky
Hi guys,
I'm new to lazarus and have a quick question. How do you launch an app from
within A lazarus Application. In delphi you could use winexec, or the
ExecuteFile method(in The FMXUtils unit). I would lie this to be platform
independent. I would appreciate any help.
There is a nice tutorial about this on the wiki at http://lazarus-ccr.sf.net
The tutorial guides you through tprocess which is the recommended class here
as it is completely platform neutral.

Ciao
A.J.
PS. I have some utility wrapper procedures in my util unit which implement
tprocess in the background but give you a very simple one-line call for using
it in many cases, if you would like I can mail them to you.
--
"80% Of a hardware engineer's job is application of the uncertainty principle.
80% of a software engineer's job is pretending this isn't so."
A.J. Venter
Chief Software Architect
OpenLab International
http://www.getopenlab.com | +27 82 726 5103 (South Africa)
http://www.silentcoder.co.za | +55 118 162 2079 (Brazil)
Sam Washkansky
2006-03-07 12:49:44 UTC
Permalink
Thanks a lot I would appreciate that.
Cheers
Sam
Post by Sam Washkansky
Post by Sam Washkansky
Hi guys,
I'm new to lazarus and have a quick question. How do you launch an app
from
Post by Sam Washkansky
within A lazarus Application. In delphi you could use winexec, or the
ExecuteFile method(in The FMXUtils unit). I would lie this to be
platform
Post by Sam Washkansky
independent. I would appreciate any help.
There is a nice tutorial about this on the wiki at
http://lazarus-ccr.sf.net
The tutorial guides you through tprocess which is the recommended class here
as it is completely platform neutral.
Ciao
A.J.
PS. I have some utility wrapper procedures in my util unit which implement
tprocess in the background but give you a very simple one-line call for using
it in many cases, if you would like I can mail them to you.
--
"80% Of a hardware engineer's job is application of the uncertainty principle.
80% of a software engineer's job is pretending this isn't so."
A.J. Venter
Chief Software Architect
OpenLab International
http://www.getopenlab.com | +27 82 726 5103 (South Africa)
http://www.silentcoder.co.za | +55 118 162 2079 (Brazil)
_________________________________________________________________
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives
--
Sam Washkansky
Tel:+27+823738257
email:***@gmail.com
A.J. Venter
2006-03-07 13:48:38 UTC
Permalink
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, I´m not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
overloaded):

procedure execute (command:string; var output:Tstrings);
{Run command and store the output from it in output
Notes:
1) do not create output prior to assigning it
2) you must free output yourself
}
const
READ_BYTES = 2048;

Var
Process :Tprocess;
MemStream : TMemoryStream;
n: LongInt;
BytesRead: LongInt;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;



{Actually run the thing and catch the output}
MemStream := TMemoryStream.Create;
outPut := TStringList.Create;
BytesRead := 0;

Process.Options := [poUsePipes,poNoConsole];

Process.Execute;

while Process.Running do
begin
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);

// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end
else begin
// no data, wait 100 ms
Sleep(100);
end;
end;
// read last part
repeat
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);
// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end;
until n <= 0;
MemStream.SetSize(BytesRead);
OutPut.LoadFromStream(MemStream);
MemStream.Free;
Process.Free;
end;

procedure execute (command:string);
{Execute command, do not wait for it to exit, do
not capture output.}

Var
Process :Tprocess;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;
Process.Options := [poNoConsole];
Process.Execute;
Process.Free;
end;
--
"80% Of a hardware engineer's job is application of the uncertainty principle.
80% of a software engineer's job is pretending this isn't so."
A.J. Venter
Chief Software Architect
OpenLab International
http://www.getopenlab.com | +27 82 726 5103 (South Africa)
http://www.silentcoder.co.za | +55 118 162 2079 (Brazil)
Mattias Gaertner
2006-03-07 13:56:14 UTC
Permalink
On Tue, 7 Mar 2006 15:48:38 +0200
Post by A.J. Venter
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, I´m not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?


Mattias
Post by A.J. Venter
procedure execute (command:string; var output:Tstrings);
{Run command and store the output from it in output
1) do not create output prior to assigning it
2) you must free output yourself
}
const
READ_BYTES = 2048;
Var
Process :Tprocess;
MemStream : TMemoryStream;
n: LongInt;
BytesRead: LongInt;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;
{Actually run the thing and catch the output}
MemStream := TMemoryStream.Create;
outPut := TStringList.Create;
BytesRead := 0;
Process.Options := [poUsePipes,poNoConsole];
Process.Execute;
while Process.Running do
begin
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);
// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end
else begin
// no data, wait 100 ms
Sleep(100);
end;
end;
// read last part
repeat
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);
// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end;
until n <= 0;
MemStream.SetSize(BytesRead);
OutPut.LoadFromStream(MemStream);
MemStream.Free;
Process.Free;
end;
procedure execute (command:string);
{Execute command, do not wait for it to exit, do
not capture output.}
Var
Process :Tprocess;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;
Process.Options := [poNoConsole];
Process.Execute;
Process.Free;
end;
Vincent Snijders
2006-03-07 14:14:12 UTC
Permalink
Post by Mattias Gaertner
On Tue, 7 Mar 2006 15:48:38 +0200
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, IŽm not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?
I made this last summer, I never got time to update the wiki. It is a sample
application with TProcesses. The examples has two plink (ssh client) command lines.
You can type the commands in the memo and see the output.

Vincent.
Mattias Gaertner
2006-03-07 14:40:32 UTC
Permalink
On Tue, 07 Mar 2006 15:14:12 +0100
Post by Vincent Snijders
Post by Mattias Gaertner
On Tue, 7 Mar 2006 15:48:38 +0200
Post by A.J. Venter
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, I´m not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?
I made this last summer, I never got time to update the wiki. It is a
sample application with TProcesses. The examples has two plink (ssh
client) command lines. You can type the commands in the memo and see the
output.
Thanks. I will try and if it works add it to the wiki.

Mattias
Vincent Snijders
2006-03-08 08:41:42 UTC
Permalink
Post by Mattias Gaertner
On Tue, 07 Mar 2006 15:14:12 +0100
Post by Vincent Snijders
Post by Mattias Gaertner
On Tue, 7 Mar 2006 15:48:38 +0200
Post by A.J. Venter
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, I´m not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?
I made this last summer, I never got time to update the wiki. It is a
sample application with TProcesses. The examples has two plink (ssh
client) command lines. You can type the commands in the memo and see the
output.
Thanks. I will try and if it works add it to the wiki.
If you make any changes to the sources, please send me a copy. If you put a
description on the wiki, I will upload the complete example to the sf download site.

Vincent
SteveG
2006-03-08 12:06:06 UTC
Permalink
Reading the source, it would seem that the line
'Process.Input.Write(InputStrings[1], length(InputStrings));' sends
whatever is contained in 'inputstrings' to the process as though
typed on the keyboard - is this correct?
if yes, then I am having problems both with my test program, and the
supplied attachment - neither cause any response from the program I am
trying to control
if no, could somebody pls explain / point me to an example to show how
to send keystrokes to a running TProcess ?

Using WinXP
Thanks
Post by Mattias Gaertner
On Tue, 07 Mar 2006 15:14:12 +0100
Post by Vincent Snijders
Post by Mattias Gaertner
On Tue, 7 Mar 2006 15:48:38 +0200
Post by A.J. Venter
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, I´m not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
Has anyone an example, how to pipe some text to a process. For example
piping some text to a 'grep' command?
I made this last summer, I never got time to update the wiki. It is a
sample application with TProcesses. The examples has two plink (ssh
client) command lines. You can type the commands in the memo and see the
output.
Thanks. I will try and if it works add it to the wiki.
Mattias
_________________________________________________________________
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives
Vincent Snijders
2006-03-08 12:30:10 UTC
Permalink
Post by SteveG
Reading the source, it would seem that the line
'Process.Input.Write(InputStrings[1], length(InputStrings));' sends
whatever is contained in 'inputstrings' to the process as though
typed on the keyboard - is this correct?
It sends them to stdin.

Can you run your controlled app by sending it a textfile to stdin?
Try this is dos-box:

c:\>MyControlledApp < MyKeyStrokes.txt.

If this doesn't work, then the TProcess won't work either.
Post by SteveG
if yes, then I am having problems both with my test program, and the
supplied attachment - neither cause any response from the program I am
trying to control
if no, could somebody pls explain / point me to an example to show how
to send keystrokes to a running TProcess ?
I don't know about sending key strokes.

Vincent
Marc Weustink
2006-03-08 13:35:13 UTC
Permalink
Post by SteveG
Reading the source, it would seem that the line
'Process.Input.Write(InputStrings[1], length(InputStrings));' sends
whatever is contained in 'inputstrings' to the process as though
typed on the keyboard - is this correct?
It is send to stdin. A sidenote here however.
The same limitations on the input pipe counts here as on the output. So
that means that if you send to much the pipe may be full and your app
will block. If the process starts to generate output based on this
input, it may fill up the output pipe, which isn't read. So your process
blocks as well -> deadlock

Marc

Sam Washkansky
2006-03-07 14:17:27 UTC
Permalink
Thanks a stack, it did the trick.
Post by Sam Washkansky
Thanks a lot I would appreciate that.
Cheers
Sam
Here you go, IŽm not going to post the whole huge unit now, just the two
procedures, you can use them as you see fit, they are pretty much just
adaptations of the examples in the tutorial anyway (note that they are
procedure execute (command:string; var output:Tstrings);
{Run command and store the output from it in output
1) do not create output prior to assigning it
2) you must free output yourself
}
const
READ_BYTES = 2048;
Var
Process :Tprocess;
MemStream : TMemoryStream;
n: LongInt;
BytesRead: LongInt;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;
{Actually run the thing and catch the output}
MemStream := TMemoryStream.Create;
outPut := TStringList.Create;
BytesRead := 0;
Process.Options := [poUsePipes,poNoConsole];
Process.Execute;
while Process.Running do
begin
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);
// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end
else begin
// no data, wait 100 ms
Sleep(100);
end;
end;
// read last part
repeat
// make sure we have room
MemStream.SetSize(BytesRead + READ_BYTES);
// try reading it
n := Process.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if n > 0
then begin
Inc(BytesRead, n);
end;
until n <= 0;
MemStream.SetSize(BytesRead);
OutPut.LoadFromStream(MemStream);
MemStream.Free;
Process.Free;
end;
procedure execute (command:string);
{Execute command, do not wait for it to exit, do
not capture output.}
Var
Process :Tprocess;
begin
Process := TProcess.create(nil);;
Process.CommandLine := command;
Process.Options := [poNoConsole];
Process.Execute;
Process.Free;
end;
--
"80% Of a hardware engineer's job is application of the uncertainty principle.
80% of a software engineer's job is pretending this isn't so."
A.J. Venter
Chief Software Architect
OpenLab International
http://www.getopenlab.com | +27 82 726 5103 (South Africa)
http://www.silentcoder.co.za | +55 118 162 2079 (Brazil)
_________________________________________________________________
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives
--
Sam Washkansky
Tel:+27+823738257
email:***@gmail.com
Loading...