How do I run an external application with Free Pascal/Lazarus?

6k views Asked by At

How do I run an external application with Free Pascal/Lazarus (using Windows)? I found the "official" reference page, with several implementations and examples. Although I'm sure it works for many people, I, with my current knowledge level, am some what lost (I don't have much routine programming with Free Pascal yet, and other examples I found on the web didn't work for me).

Is there a "clear" example that helps me to do the "first steps"? Thanks.

2

There are 2 answers

0
Marco van de Voort On BEST ANSWER

If you don't need piping you can just use execute process.

uses sysutils;
begin
  executeprocess('notepad.exe',['document.txt']);
end.
3
Albin On

Here's a working example (source) using TProcess:

uses Process;
var
  RunProgram: TProcess;
begin
  RunProgram := TProcess.Create(nil);
  RunProgram.CommandLine := ‘Path and Name of Program’;
  RunProgram.Execute;
  RunProgram.Free;
end;

For example, this will open the application "MS Notepad":

uses Process;
var
  RunProgram: TProcess;
begin
  RunProgram := TProcess.Create(nil);
  RunProgram.CommandLine := ‘notepad.exe’;
  RunProgram.Execute;
  RunProgram.Free;
end;