Difference between revisions of "Executing External Programs"

From Free Pascal wiki
Jump to navigationJump to search
m (fixed spelling)
(changed from notepad to ppc386 for output)
Line 28: Line 28:
 
   
 
   
 
   // Tell the new AProcess what the command to execute is.
 
   // Tell the new AProcess what the command to execute is.
   AProcess.CommandLine := 'c:\windows\notepad.exe';
+
  // Let's use the FreePascal compiler
 +
   AProcess.CommandLine := 'ppc386 -h';
 
   
 
   
 
   // We will define an option for when the program
 
   // We will define an option for when the program
Line 40: Line 41:
 
   AProcess.Execute;
 
   AProcess.Execute;
 
   
 
   
   // This is not reached until notepad stops running.
+
   // This is not reached until ppc386 stops running.
 
   AProcess.Free;   
 
   AProcess.Free;   
 
  end.
 
  end.
Line 79: Line 80:
 
   
 
   
 
   // Tell the new AProcess what the command to execute is.
 
   // Tell the new AProcess what the command to execute is.
   AProcess.CommandLine := 'c:\windows\notepad.exe';
+
  // Let's use the FreePascal compiler
 +
   AProcess.CommandLine := 'ppc386 -h';
 
   
 
   
 
   // We will define an option for when the program
 
   // We will define an option for when the program
Line 92: Line 94:
 
   AProcess.Execute;
 
   AProcess.Execute;
 
    
 
    
   // This is not reached until notepad stops running.
+
   // This is not reached until ppc386 stops running.
 
   
 
   
 
   // Now read the output of the program we just ran
 
   // Now read the output of the program we just ran

Revision as of 16:59, 8 March 2005

There are multiple ways to run an external program, but I will only focus on one here. TProcess.

You can use TProcess to launch external programs. Some of the benefits of using TProcess are that it is:

  • Platform Independant
  • Capable of reading from stdout and writing to stdin.

A simple example:

// This is a demo program that shows how to launch
// an external program.
program launchprogram;

// Here we include files that have useful functions
// and procedures we will need.
uses 
  Classes, SysUtils, Process;

// This is defining the var "AProcess" as a variable 
// of the type "TProcess"
var 
  AProcess: TProcess;

// This is where our program starts to run
begin
  // Now we will create the TProcess object, and
  // assign it to the var AProcess.
  AProcess := TProcess.Create(nil);

  // Tell the new AProcess what the command to execute is.
  // Let's use the FreePascal compiler
  AProcess.CommandLine := 'ppc386 -h';

  // We will define an option for when the program
  // is run. This option will make sure that our program
  // does not continue until the program we will launch
  // has stopped running.                vvvvvvvvvvvvvv
  AProcess.Options := AProcess.Options + [poWaitOnExit];

  // Now that AProcess knows what the commandline is 
  // we will run it.
  AProcess.Execute;

  // This is not reached until ppc386 stops running.
  AProcess.Free;   
end.

That's it! You have just learned how to run an external program from inside your own program.


That's nice, but how do I read the Output of a program that I have run?

Well, let's expand our example a little and do just that:

// This is a demo program that shows how to launch
// an external program and read from it's output.
program launchprogram;

// Here we include files that have useful functions
// and procedures we will need.
uses 
  Classes, SysUtils, Process;

// This is defining the var "AProcess" as a variable 
// of the type "TProcess"
// Also now we are adding a TStringList to store the 
// data read from the programs output.
var 
  AProcess: TProcess;
  AStringList: TStringList;

// This is where our program starts to run
begin
  // Now we will create the TProcess object, and
  // assign it to the var AProcess.
  AProcess := TProcess.Create(nil);

  // Create the TStringList object.
  AStringList := TStringList.Create;

  // Tell the new AProcess what the command to execute is.
  // Let's use the FreePascal compiler
  AProcess.CommandLine := 'ppc386 -h';

  // We will define an option for when the program
  // is run. This option will make sure that our program
  // does not continue until the program we will launch
  // has stopped running. Also now we will tell it that
  // we want to read the output of the file.
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];

  // Now that AProcess knows what the commandline is 
  // we will run it.
  AProcess.Execute;
  
  // This is not reached until ppc386 stops running.

  // Now read the output of the program we just ran
  // into the TStringList.
  AStringList.Lines.ReadFromStream(AProcess.Output);
  
  // Save the output to a file.
  AStringList.Lines.SaveToFile('c:\output.txt');

  // Now that the file is saved we can free the 
  // TStringList and the TProcess.
  AStringList.Free;
  AProcess.Free;   
end.