Difference between revisions of "Executing External Programs/nl"

From Free Pascal wiki
Jump to navigationJump to search
Line 18: Line 18:
 
   Classes, SysUtils, Process;
 
   Classes, SysUtils, Process;
 
   
 
   
  // This is defining the var "AProcess" as a variable
+
  // Hier definieren we de variabele aProcess van het
  // of the type "TProcess"
+
  // type "TProcess"
 
  var  
 
  var  
   AProcess: TProcess;
+
   aProcess: TProcess;
 
   
 
   
  // This is where our program starts to run
+
  // Hier begint ons programma
 
  begin
 
  begin
   // Now we will create the TProcess object, and
+
   // Allereerst maken we een instantie van het TProcess
   // assign it to the var AProcess.
+
   // object en kennen dat toe aan aProcess
   AProcess := TProcess.Create(nil);
+
   aProcess := TProcess.Create(nil);
 
   
 
   
   // Tell the new AProcess what the command to execute is.
+
   // Welk commando gaan we uitvoeren?
   // Let's use the FreePascal compiler
+
   // Voor deze keer voeren we de FPC compiler uit.
 
   AProcess.CommandLine := 'ppc386 -h';
 
   AProcess.CommandLine := 'ppc386 -h';
 
   
 
   
   // We will define an option for when the program
+
   // Nu definieren we wat er moet gebueren nadat het
   // is run. This option will make sure that our program
+
   // programma is gestart. In dit geval wachten we met
   // does not continue until the program we will launch
+
   // ons programma totdat het gestarte programma is   
   // has stopped running.               vvvvvvvvvvvvvv
+
   // afgelopen.                         vvvvvvvvvvvvvv
 
   AProcess.Options := AProcess.Options + [poWaitOnExit];
 
   AProcess.Options := AProcess.Options + [poWaitOnExit];
 
   
 
   
   // Now that AProcess knows what the commandline is
+
   // Nu weet aProcess wat er moet gebeuren, dus laten
   // we will run it.
+
   // we het uitvoeren.
 
   AProcess.Execute;
 
   AProcess.Execute;
 
   
 
   
   // This is not reached until ppc386 stops running.
+
   // Hier komen we dus pas als ppc386 gestopt is.
 
   AProcess.Free;   
 
   AProcess.Free;   
 
  end.
 
  end.
  
That's it! You have just learned how to run an external program from inside your own program.
+
Simpel he! Je hebt nu gezien hoe je een ander programma kunt uitvoeren vanuit je eigen programma!
  
  

Revision as of 00:46, 25 March 2005

Inleiding

Er zijn verschillende manieren om een extern programma uit te laten voeren. We richten ons nu op een manier, namelijk het gebruik van TProcess.

TProcess

TProcess wordt gebruikt om een extern programma uit te laten voeren. Een paar voordelen van het gebruik van TProcess boven andere manieren zijn:

  • Platform onafhankelijk.
  • Het geeft ook de mogelijkheid om te schrijven naar stdout en te lezen van stdin.

Een eenvoudig voorbeeld

// Dit is een demo prograamma dat laat zien hoe je
// een extern programma kunt uitvoeren.
program launchprogram;

// De te gebruiken units. 
uses 
  Classes, SysUtils, Process;

// Hier definieren we de variabele aProcess van het
// type "TProcess"
var 
  aProcess: TProcess;

// Hier begint ons programma
begin
  // Allereerst maken we een instantie van het TProcess
  // object en kennen dat toe aan aProcess
  aProcess := TProcess.Create(nil);

  // Welk commando gaan we uitvoeren?
  // Voor deze keer voeren we de FPC compiler uit.
  AProcess.CommandLine := 'ppc386 -h';

  // Nu definieren we wat er moet gebueren nadat het
  // programma is gestart. In dit geval wachten we met
  // ons programma totdat het gestarte programma is    
  // afgelopen.                          vvvvvvvvvvvvvv
  AProcess.Options := AProcess.Options + [poWaitOnExit];

  // Nu weet aProcess wat er moet gebeuren, dus laten
  // we het uitvoeren.
  AProcess.Execute;

  // Hier komen we dus pas als ppc386 gestopt is.
  AProcess.Free;   
end.

Simpel he! Je hebt nu gezien hoe je een ander programma kunt uitvoeren vanuit je eigen programma!


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:

An Improved Example

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