Difference between revisions of "Executing External Programs/it"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(16 intermediate revisions by 5 users not shown)
Line 2: Line 2:
  
 
== Introduzione ==
 
== Introduzione ==
Ci sono molti modi di eseguire un programma esterno, ma questo articolo si focalizza solo su uno: [[doc:fcl/process/tprocess.html|TProcess]].
+
Ci sono diversi modi di eseguire un programma esterno, ma questo articolo ne esamina solo uno: [[doc:fcl/process/tprocess.html|TProcess]].
Se usi solitamente '''ShellExecute''' e / o  '''WinExec''' in Delphi, allora puoi iniziare ad usare  TProcess come un'alternativa in FPC/Lazarus (Questo vale anche se usi usi  Lazarus in Linux, perchè TProcess è cross-platform).
 
  
'''Nota:''' FPC/Lazarus  supporta  '''ShellExecute''' e / o '''WinExec''', ma questo supporto è solo in Win32. Se vuoi programmare in 'cross-platform', allora usa TProcess, è la miglior scelta!
+
Se solitamente usi '''ShellExecute''' e/o '''WinExec''' in Delphi, puoi iniziare ad usare  TProcess come un'alternativa in FPC/Lazarus (vale anche se usi Lazarus in Linux, perché TProcess è cross-platform).
  
 +
'''Nota:''' FPC/Lazarus  supporta  '''ShellExecute''' e/o '''WinExec''', ma questo supporto è solo in  Win32. Se vuoi programmare 'cross-platform', allora usa TProcess, è la miglior scelta!
  
 
== SysUtils.ExecuteProcess ==
 
== SysUtils.ExecuteProcess ==
  
Il modo più semplice se non necessiti  pipes ne ogni forma di controllo è di usare semplicemente SysUtils.ExecuteProcess('/percorso/completo/al/file/binario',['arg1','arg2']);
+
Se non hai bisogno di 'pipes' né di altre forme di controllo, il sistema più semplice è quello di usare  SysUtils.ExecuteProcess('/percorso/completo/al/file/binario',['arg1','arg2']);
 
 
  
 
== TProcess ==
 
== TProcess ==
Puoi usare  TProcess per lanciare programmi esterni.Alcuni benefici nell'uso di  TProcess sono:
+
Puoi usare  TProcess per lanciare programmi esterni. Alcuni vantaggi nell'uso di  TProcess sono:
  
 
*Piattaforma Indipendente
 
*Piattaforma Indipendente
*Capacità di leggere da stdout e scrivere in  stdin.
+
*Capacità di leggere da stdout e scrivere in  stdin.
  
Nota: TProcess non è un  terminal/shell! Non puoi eseguire direttamente scripts or redirigere output usando operatori tipo "|", ">", "<", "&" etc. possibile ottenere gli stessi risultati con TProcess usando Pascal, qualche sempio è di seguito..
+
Nota: TProcess non è un  terminal/shell! Non puoi eseguire direttamente scripts or redirigere output usando operatori tipo "|", ">", "<", "&" etc. E' possibile ottenere gli stessi risultati con TProcess usando Pascal; puoi vedere alcuni esempi più avanti.
  
Importante: Devi specificare il percorso completo all'eseguibile. Per esempio '/bin/cp' invece di 'cp'. Se il programma è nel percorso standard PATH allora puoi usare la funzione  [[doc:lcl/fileutil/finddefaultexecutablepath.html|FindDefaultExecutablePath]] da [[doc:lcl/fileutil/index.html|FileUtil]] unit di LCL.
+
Importante: Devi specificare il percorso completo dell'eseguibile. Per esempio '/bin/cp' invece di 'cp'. Se il programma è nel percorso standard PATH allora puoi usare la funzione  [[doc:lcl/fileutil/finddefaultexecutablepath.html|FindDefaultExecutablePath]] dalla unit [[doc:lcl/fileutil/index.html|FileUtil]] di LCL.
  
 
=== Un semplice esempio ===
 
=== Un semplice esempio ===
<pascal>
+
 
 +
<syntaxhighlight lang=pascal>
 
  // Questo è un programma dimostrativo che mostra come lanciare
 
  // Questo è un programma dimostrativo che mostra come lanciare
  // un mprogramma esterno.
+
  // un programma esterno.
 
  program launchprogram;
 
  program launchprogram;
 
   
 
   
Line 34: Line 34:
 
   Classes, SysUtils, Process;
 
   Classes, SysUtils, Process;
 
   
 
   
  // This is defining the var "AProcess" as a variable
+
  // Questa è la definizione della variabile "AProcess"  
  // of the type "TProcess"
+
  // come una variabile di tipo "TProcess"
 
  var  
 
  var  
 
   AProcess: TProcess;
 
   AProcess: TProcess;
 
   
 
   
  // This is where our program starts to run
+
  // Qui è dove il nostro programma inizia
 
  begin
 
  begin
   // Now we will create the TProcess object, and
+
   // Adesso creeremo l'oggetto TProcess e
   // assign it to the var AProcess.
+
   // lo assegneremo  alla variabile  var AProcess.
 
   AProcess := TProcess.Create(nil);
 
   AProcess := TProcess.Create(nil);
 
   
 
   
   // Tell the new AProcess what the command to execute is.
+
   // Diremo al nuovo  AProcess quale è il comando da eseguire.
   // Let's use the FreePascal compiler
+
   // Usa il compilatore  FreePascal
 
   AProcess.CommandLine := 'ppc386 -h';
 
   AProcess.CommandLine := 'ppc386 -h';
 
   
 
   
   // We will define an option for when the program
+
   // Definiremo una opzione per quando il programma
   // is run. This option will make sure that our program
+
   // è in esecuzione. Questa opzione ci assicurerà che il nostro programma
   // does not continue until the program we will launch
+
   // non continuerà sin quando il programma che lanceremo
   // has stopped running.                vvvvvvvvvvvvvv
+
   // non sarà terminato.                vvvvvvvvvvvvvv
 
   AProcess.Options := AProcess.Options + [poWaitOnExit];
 
   AProcess.Options := AProcess.Options + [poWaitOnExit];
 
   
 
   
   // Now that AProcess knows what the commandline is
+
   // Adesso che AProcess sa qual'è la linea di comando
   // we will run it.
+
   // verrà lanciato.
 
   AProcess.Execute;
 
   AProcess.Execute;
 
   
 
   
   // This is not reached until ppc386 stops running.
+
   // Questo non avverrà sino a quando ppc386 non fermerà la sua esecuzione.
 
   AProcess.Free;   
 
   AProcess.Free;   
 
  end.
 
  end.
</pascal>
+
</syntaxhighlight>
Questo è quanto! Hai appena imparato come eseguire un programma esterno dall'interno del tuo stesso programma.
+
Questo è quanto! Hai appena imparato come eseguire un programma esterno dall'interno del tuo programma.
  
=== Un esempio comprovato ===
+
=== Un esempio più elaborato ===
Che bello!, ma come leggo l' Output di un programma che ho eseguito ?
+
Che bello!, ma come faccio a leggere l'Output di un programma che ho eseguito?
  
Bene, espandiamo il nostro esempio cosi che otteniamo questo:
+
Bene, sviluppiamo un po' il nostro esempio in modo da ottenere questo:
  
<delphi>
+
<syntaxhighlight lang=pascal>
  // This is a demo program that shows how to launch
+
  // Questo è un programma dimostrativo che mostra come
  // an external program and read from it's output.
+
  // lanciare un programma esterno e leggere dal suo output.
 
  program launchprogram;
 
  program launchprogram;
 
   
 
   
  // Here we include files that have useful functions
+
  // Qui mettiamo i files che contengono funzioni utili
  // and procedures we will need.
+
  // e le procedure di cui abbiamo bisogno.
 
  uses  
 
  uses  
 
   Classes, SysUtils, Process;
 
   Classes, SysUtils, Process;
 
   
 
   
  // This is defining the var "AProcess" as a variable
+
  // Questa è la definizione della variabile "AProcess"
  // of the type "TProcess"
+
  // come variabile del tipo "TProcess"
  // Also now we are adding a TStringList to store the
+
  // Aggiungiamo anche una TStringList per memorizzare
  // data read from the programs output.
+
  // i dati che leggeremo dall'output del programma.
 
  var  
 
  var  
 
   AProcess: TProcess;
 
   AProcess: TProcess;
 
   AStringList: TStringList;
 
   AStringList: TStringList;
 
   
 
   
  // This is where our program starts to run
+
  // Questo è l'inizio del programma
 
  begin
 
  begin
   // Now we will create the TProcess object, and
+
   // Ora creeremo l'oggetto TProcess, e lo
   // assign it to the var AProcess.
+
   // assegneremo alla variabile AProcess.
 
   AProcess := TProcess.Create(nil);
 
   AProcess := TProcess.Create(nil);
 
   
 
   
   // Create the TStringList object.
+
   // Creazione dell'oggetto TStringList.
 
   AStringList := TStringList.Create;
 
   AStringList := TStringList.Create;
 
   
 
   
   // Tell the new AProcess what the command to execute is.
+
   // Diciamo al nuovo AProcess quale comando eseguire.
   // Let's use the FreePascal compiler
+
   // Utilizza il compilatore FreePascal
 
   AProcess.CommandLine := 'ppc386 -h';
 
   AProcess.CommandLine := 'ppc386 -h';
 
   
 
   
   // We will define an option for when the program
+
   // Definiamo un'opzione per il programma durante
   // is run. This option will make sure that our program
+
   // l'esecuzione. Questa opzione garantisce che il nostro
   // does not continue until the program we will launch
+
   // programma rimane in attesa finché il programma che
   // has stopped running. Also now we will tell it that
+
   // abbiamo lanciato non si ferma. Gli diciamo anche che
   // we want to read the output of the file.
+
   // vogliamo leggere l'output.
 
   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
 
   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
 
   
 
   
   // Now that AProcess knows what the commandline is
+
   // Ora che AProcess qual'è la linea di comando
   // we will run it.
+
   // lo eseguiamo.
 
   AProcess.Execute;
 
   AProcess.Execute;
 
    
 
    
   // This is not reached until ppc386 stops running.
+
   // Quanto segue non viene eseguito finché ppc386 non
 +
  // termina l'esecuzione.
 
   
 
   
   // Now read the output of the program we just ran
+
   // Ora leggiamo l'output del programma che abbiamo
   // into the TStringList.
+
   // appena eseguito all'interno della TStringList.
 
   AStringList.LoadFromStream(AProcess.Output);
 
   AStringList.LoadFromStream(AProcess.Output);
 
    
 
    
   // Save the output to a file.
+
   // Salviamo l'output in un file.
 
   AStringList.SaveToFile('output.txt');
 
   AStringList.SaveToFile('output.txt');
 
   
 
   
   // Now that the file is saved we can free the
+
   // Ora che il file è stato salvato, possiamo liberare
   // TStringList and the TProcess.
+
   // la TStringList e il TProcess.
 
   AStringList.Free;
 
   AStringList.Free;
 
   AProcess.Free;   
 
   AProcess.Free;   
 
  end.
 
  end.
</delphi>
+
</syntaxhighlight>
  
=== Reading large output ===
+
=== Leggere output di grandi dimensioni ===
In the previous example we waited until the program exited. Then we read, what the program has written to its output. But suppose the program writes a lot of data to the output, the pipe becomes full and the called progam waits until the pipe has been read from. But the calling program doesn't read from it, until the called program has ended. A dead lock occurs.
+
Nel precedente esempio noi abbiamo aspettato fino all'uscita del programma. Quindi leggiamo, quello che il programma ha scritto nel suo output. Ma supponiamo che il programma scriva molti dati nell'output, il buffer si riempie e il programma richiamato aspetta sinché il buffer non viene letto. Ma il programma chiamante non legge dal buffer, sinché il programma chiamato non ha terminato. Avviene un blocco.
  
The following example therefore doesn't use poWaitOnExit, but reads from the output, while the program is still running. The output is stored in a memory stream, that can be used later to read the output into a TStringList.
+
Il seguente esempio quindi non usa  poWaitOnExit, ma legge dall'output, mentre il programma è ancora in esecuzione. L'output è immagazzinato in un buffer di memoria, che potrà essere usato  più tardi per leggere  l'output all'interno di una  TStringList.
  
<delphi> program procoutlarge;
+
<syntaxhighlight lang=pascal>
 +
program procoutlarge;
 
  {
 
  {
 
     Copyright (c) 2004 by Marc Weustink
 
     Copyright (c) 2004 by Marc Weustink
Line 216: Line 218:
 
   P.Free;
 
   P.Free;
 
   M.Free;
 
   M.Free;
  end.</delphi>
+
  end.</syntaxhighlight>
 +
 
 +
=== Usare input e output di TProcess ===
 +
Vedi l'esempio dimostrativo  in  [https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/examples/process Lazarus-CCR SVN].
  
=== Using input and output of a TProcess ===
+
=== Accorgimenti nell'uso di  TProcess ===
See processdemo example in the [https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/examples/process Lazarus-CCR SVN].
+
Se stai creando un programma  multipiattaforma (cross-platform) , Puoi cambiare la linea di comando in base al Sistema Operativo , usando le direttive "{$IFDEF}s" and "{$ENDIF}s".
  
=== Hints on the use of TProcess ===
+
Esempio:
If you are creating a cross-platform program, you can change commandline according to the OS, using directives "{$IFDEF}s" and "{$ENDIF}s".
 
  
Example:
+
<syntaxhighlight lang=pascal>
<delphi> {...}
+
{...}
 
   AProcess:TProcess.Create(nil)
 
   AProcess:TProcess.Create(nil)
 
   {$IFDEF WIN32}
 
   {$IFDEF WIN32}
Line 234: Line 238:
 
   {$ENDIF}
 
   {$ENDIF}
 
   AProcess.Execute; //in alternative, you can use AProcess.Active:=True
 
   AProcess.Execute; //in alternative, you can use AProcess.Active:=True
  {...}</delphi>
+
  {...}</syntaxhighlight>
  
=== Example of "talking" with aspell process ===
+
=== Esempio di dialogo con il processo  aspell ===
  
Inside [http://pasdoc.sourceforge.net/ pasdoc] source code you can find two units that perform spell-checking by "talking" with running aspell process through pipes:
+
All'interno di  [http://pasdoc.sourceforge.net/ pasdoc] Codice sorgente puoi trovare due  units che  fanno spell-checking mediante  "dialogo" con  aspell in esecuzione attraverso pipes:
  
 
* [http://pasdoc.svn.sourceforge.net/viewvc/*checkout*/pasdoc/trunk/source/component/PasDoc_ProcessLineTalk.pas PasDoc_ProcessLineTalk.pas unit] implements TProcessLineTalk class, descendant of TProcess, that can be easily used to talk with any process on a line-by-line basis.
 
* [http://pasdoc.svn.sourceforge.net/viewvc/*checkout*/pasdoc/trunk/source/component/PasDoc_ProcessLineTalk.pas PasDoc_ProcessLineTalk.pas unit] implements TProcessLineTalk class, descendant of TProcess, that can be easily used to talk with any process on a line-by-line basis.
Line 244: Line 248:
 
* [http://pasdoc.svn.sourceforge.net/viewvc/*checkout*/pasdoc/trunk/source/component/PasDoc_Aspell.pas PasDoc_Aspell.pas units] implements TAspellProcess class, that performs spell-checking by using underlying TProcessLineTalk instance to execute aspell and communicate with running aspell process.
 
* [http://pasdoc.svn.sourceforge.net/viewvc/*checkout*/pasdoc/trunk/source/component/PasDoc_Aspell.pas PasDoc_Aspell.pas units] implements TAspellProcess class, that performs spell-checking by using underlying TProcessLineTalk instance to execute aspell and communicate with running aspell process.
  
Both units are rather independent from the rest of pasdoc sources, so they may serve as real-world examples of using TProcess to run and communicate through pipes with other program.
+
Entrambe le units sono indipendenti dal resto di pasdoc sorgente, cosi che esse possono servire come esempio reale dell'uso di TProcess per eseguire e comunicare attraverso pipes con altri programmi.
 
 
----
 
[[TXMLPropStorage]]
 

Latest revision as of 13:57, 14 February 2020

Deutsch (de) English (en) español (es) français (fr) italiano (it) 日本語 (ja) Nederlands (nl) polski (pl) português (pt) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

Introduzione

Ci sono diversi modi di eseguire un programma esterno, ma questo articolo ne esamina solo uno: TProcess.

Se solitamente usi ShellExecute e/o WinExec in Delphi, puoi iniziare ad usare TProcess come un'alternativa in FPC/Lazarus (vale anche se usi Lazarus in Linux, perché TProcess è cross-platform).

Nota: FPC/Lazarus supporta ShellExecute e/o WinExec, ma questo supporto è solo in Win32. Se vuoi programmare 'cross-platform', allora usa TProcess, è la miglior scelta!

SysUtils.ExecuteProcess

Se non hai bisogno di 'pipes' né di altre forme di controllo, il sistema più semplice è quello di usare SysUtils.ExecuteProcess('/percorso/completo/al/file/binario',['arg1','arg2']);

TProcess

Puoi usare TProcess per lanciare programmi esterni. Alcuni vantaggi nell'uso di TProcess sono:

  • Piattaforma Indipendente
  • Capacità di leggere da stdout e scrivere in stdin.

Nota: TProcess non è un terminal/shell! Non puoi eseguire direttamente scripts or redirigere output usando operatori tipo "|", ">", "<", "&" etc. E' possibile ottenere gli stessi risultati con TProcess usando Pascal; puoi vedere alcuni esempi più avanti.

Importante: Devi specificare il percorso completo dell'eseguibile. Per esempio '/bin/cp' invece di 'cp'. Se il programma è nel percorso standard PATH allora puoi usare la funzione FindDefaultExecutablePath dalla unit FileUtil di LCL.

Un semplice esempio

 // Questo è un programma dimostrativo che mostra come lanciare
 // un programma esterno.
 program launchprogram;
 
 // Qui includiamo i files che hanno utilizzo funzionale
 // e le procedure che ci occorreranno.
 uses 
   Classes, SysUtils, Process;
 
 // Questa è la definizione della variabile "AProcess" 
 // come una variabile di tipo "TProcess"
 var 
   AProcess: TProcess;
 
 // Qui è dove il nostro programma inizia
 begin
   // Adesso creeremo l'oggetto TProcess e
   // lo assegneremo  alla variabile  var AProcess.
   AProcess := TProcess.Create(nil);
 
   // Diremo al nuovo  AProcess quale è il comando da eseguire.
   // Usa il compilatore  FreePascal
   AProcess.CommandLine := 'ppc386 -h';
 
   // Definiremo una opzione per quando il programma
   // è in esecuzione. Questa opzione ci assicurerà che il nostro programma
   // non continuerà sin quando il programma che lanceremo
   // non sarà terminato.                vvvvvvvvvvvvvv
   AProcess.Options := AProcess.Options + [poWaitOnExit];
 
   // Adesso che AProcess sa qual'è la linea di comando 
   // verrà lanciato.
   AProcess.Execute;
 
   // Questo non avverrà sino a quando ppc386 non fermerà la sua esecuzione.
   AProcess.Free;   
 end.

Questo è quanto! Hai appena imparato come eseguire un programma esterno dall'interno del tuo programma.

Un esempio più elaborato

Che bello!, ma come faccio a leggere l'Output di un programma che ho eseguito?

Bene, sviluppiamo un po' il nostro esempio in modo da ottenere questo:

 // Questo è un programma dimostrativo che mostra come
 // lanciare un programma esterno e leggere dal suo output.
 program launchprogram;
 
 // Qui mettiamo i files che contengono funzioni utili
 // e le procedure di cui abbiamo bisogno.
 uses 
   Classes, SysUtils, Process;
 
 // Questa è la definizione della variabile "AProcess"
 // come variabile del tipo "TProcess"
 // Aggiungiamo anche una TStringList per memorizzare
 // i dati che leggeremo dall'output del programma.
 var 
   AProcess: TProcess;
   AStringList: TStringList;
 
 // Questo è l'inizio del programma
 begin
   // Ora creeremo l'oggetto TProcess, e lo
   // assegneremo alla variabile AProcess.
   AProcess := TProcess.Create(nil);
 
   // Creazione dell'oggetto TStringList.
   AStringList := TStringList.Create;
 
   // Diciamo al nuovo AProcess quale comando eseguire.
   // Utilizza il compilatore FreePascal
   AProcess.CommandLine := 'ppc386 -h';
 
   // Definiamo un'opzione per il programma durante
   // l'esecuzione. Questa opzione garantisce che il nostro
   // programma rimane in attesa finché il programma che
   // abbiamo lanciato non si ferma. Gli diciamo anche che
   // vogliamo leggere l'output.
   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
 
   // Ora che AProcess qual'è la linea di comando 
   // lo eseguiamo.
   AProcess.Execute;
   
   // Quanto segue non viene eseguito finché ppc386 non
   // termina l'esecuzione.
 
   // Ora leggiamo l'output del programma che abbiamo
   // appena eseguito all'interno della TStringList.
   AStringList.LoadFromStream(AProcess.Output);
   
   // Salviamo l'output in un file.
   AStringList.SaveToFile('output.txt');
 
   // Ora che il file è stato salvato, possiamo liberare
   // la TStringList e il TProcess.
   AStringList.Free;
   AProcess.Free;   
 end.

Leggere output di grandi dimensioni

Nel precedente esempio noi abbiamo aspettato fino all'uscita del programma. Quindi leggiamo, quello che il programma ha scritto nel suo output. Ma supponiamo che il programma scriva molti dati nell'output, il buffer si riempie e il programma richiamato aspetta sinché il buffer non viene letto. Ma il programma chiamante non legge dal buffer, sinché il programma chiamato non ha terminato. Avviene un blocco.

Il seguente esempio quindi non usa poWaitOnExit, ma legge dall'output, mentre il programma è ancora in esecuzione. L'output è immagazzinato in un buffer di memoria, che potrà essere usato più tardi per leggere l'output all'interno di una TStringList.

 program procoutlarge;
 {
     Copyright (c) 2004 by Marc Weustink
 
     This example is creeated in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 }
 
 uses
   Classes, Process, SysUtils;
 
 const
   READ_BYTES = 2048;
   
 var
   S: TStringList;
   M: TMemoryStream;
   P: TProcess;
   n: LongInt;
   BytesRead: LongInt;
 
 begin
   // We cannot use poWaitOnExit here since we don't
   // know the size of the output. On Linux the size of the
   // output pipe is 2 kB. If the output data is more, we 
   // need to read the data. This isn't possible since we are 
   // waiting. So we get a deadlock here.
   //
   // A temp Memorystream is used to buffer the output
   
   M := TMemoryStream.Create;
   BytesRead := 0;
 
   P := TProcess.Create(nil);
   P.CommandLine := 'ppc386 -va bogus.pp';
   P.Options := [poUsePipes];
   WriteLn('-- executing --');
   P.Execute;
   while P.Running do
   begin          
     // make sure we have room
     M.SetSize(BytesRead + READ_BYTES);
     
     // try reading it
     n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
     if n > 0 
     then begin
       Inc(BytesRead, n);
       Write('.')
     end
     else begin     
       // no data, wait 100 ms
       Sleep(100); 
     end;
   end;
   // read last part
   repeat
     // make sure we have room
     M.SetSize(BytesRead + READ_BYTES);
     // try reading it
     n := P.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
     if n > 0 
     then begin
       Inc(BytesRead, n);
       Write('.');
     end;
   until n <= 0;
   if BytesRead > 0 then WriteLn;
   M.SetSize(BytesRead); 
   WriteLn('-- executed --');
   
   S := TStringList.Create;
   S.LoadFromStream(M);
   WriteLn('-- linecount = ', S.Count, ' --');
   for n := 0 to S.Count - 1 do
   begin
     WriteLn('| ', S[n]);
   end;
   WriteLn('-- end --');
   S.Free;
   P.Free;
   M.Free;
 end.

Usare input e output di TProcess

Vedi l'esempio dimostrativo in Lazarus-CCR SVN.

Accorgimenti nell'uso di TProcess

Se stai creando un programma multipiattaforma (cross-platform) , Puoi cambiare la linea di comando in base al Sistema Operativo , usando le direttive "{$IFDEF}s" and "{$ENDIF}s".

Esempio:

 {...}
   AProcess:TProcess.Create(nil)
   {$IFDEF WIN32}
   AProcess.CommandLine := 'calc.exe'; //Windows Calc
   {$ENDIF}
   {$IFDEF LINUX}
   AProcess.CommandLine := 'kcalc'; //KDE Calc
   {$ENDIF}
   AProcess.Execute; //in alternative, you can use AProcess.Active:=True
 {...}

Esempio di dialogo con il processo aspell

All'interno di pasdoc Codice sorgente puoi trovare due units che fanno spell-checking mediante "dialogo" con aspell in esecuzione attraverso pipes:

  • PasDoc_ProcessLineTalk.pas unit implements TProcessLineTalk class, descendant of TProcess, that can be easily used to talk with any process on a line-by-line basis.
  • PasDoc_Aspell.pas units implements TAspellProcess class, that performs spell-checking by using underlying TProcessLineTalk instance to execute aspell and communicate with running aspell process.

Entrambe le units sono indipendenti dal resto di pasdoc sorgente, cosi che esse possono servire come esempio reale dell'uso di TProcess per eseguire e comunicare attraverso pipes con altri programmi.