Recent

Author Topic: TProcess Syntaxe problems  (Read 2499 times)

BSaidus

  • Sr. Member
  • ****
  • Posts: 453
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
TProcess Syntaxe problems
« on: September 24, 2017, 02:20:28 pm »
Hi.
I wrote a small portion of code (simple form ) and put a code to lunch an other executable (mysqldump.exe) in order to do a mariadb databases backup ..

  So
   1. THIS CODE IS Running well :
Code: Pascal  [Select][+][-]
  1.     Fpr.Parameters.Clear;
  2.     cmdline := Format('%s --host=%s --user=%s --password=%s  --port=%d --create-options --database %s -r%s',
  3.                   [FDump, FHost, FUser, FPass, FPort, tmpDBN, tmpSQL]);
  4.     Fpr.CommandLine := cmdline;
  5.     Fpr.Execute;
  6.     while Fpr.Running do Application.ProcessMessages;
  7.  

  2. AND THIS DOESN'T:
Code: Pascal  [Select][+][-]
  1.      Fpr.Executable := FDump;
  2.      //Parameters.Add( Format(' --host=%s --user=%s --password=%s  --port=%d --create-options --database %s -r%s',
  3.                    //[FHost, FUser, FPass, FPort, tmpDBN, tmpSQL]) );
  4.      Fpr.Parameters.Add(Format('--host=%s', [FHost]));
  5.      Fpr.Parameters.Add(Format('--user=%s', [FUser]));
  6.      Fpr.Parameters.Add(Format('--password=%s', [FPass]));
  7.      Fpr.Parameters.Add(Format('--port=%d', [FPort]));
  8.      Fpr.Parameters.Add(Format('--database %s', [tmpDBN]));
  9.      Fpr.Parameters.Add(Format('-r%s',[tmpSQL]));
  10.      Fpr.Parameters.Add(' --create-options');
  11.  
  12.      //Parameters.Add(' --host=' + FHost + ' ');
  13.      //Parameters.Add(' --user=' + FUser + ' ');
  14.      //Parameters.Add(' --password=' + FPass + ' ');
  15.      //Parameters.Add(' --port=' + IntToStr(FPort) + ' ');
  16.      //Parameters.Add(' --create-options ');
  17.      //Parameters.Add(' --database ' + tmpDBN);
  18.      //Parameters.Add(' -r' + tmpSQL);
  19.  
  20.      //ShowMessage( Fpr.Executable + ' ' +  Fpr.Parameters.Text);
  21.  
  22.      Fpr.Execute;
  23.      While Fpr.Running do Application.ProcessMessages;          
  24.  
 

Where is the Problem ?
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

guest58172

  • Guest
Re: TProcess Syntaxe problems
« Reply #1 on: September 24, 2017, 02:39:57 pm »
line 10: there's leading space.
line 8: maybe split in two options (unless "=" was expected ?)

BSaidus

  • Sr. Member
  • ****
  • Posts: 453
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: TProcess Syntaxe problems
« Reply #2 on: September 24, 2017, 02:50:41 pm »
The syntaxe of the command line is ok, even if there is no leading spaces in line 10.
and there is no need for  "=" in the line 8.


Thanks.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

guest58172

  • Guest
Re: TProcess Syntaxe problems
« Reply #3 on: September 24, 2017, 03:33:22 pm »
So it was line 8 ?

Code: Pascal  [Select][+][-]
  1. Fpr.Parameters.Add(Format('--host=%s', [FHost]));
  2. Fpr.Parameters.Add(Format('--user=%s', [FUser]));
  3. Fpr.Parameters.Add(Format('--password=%s', [FPass]));
  4. Fpr.Parameters.Add(Format('--port=%d', [FPort]));
  5. Fpr.Parameters.Add('--database'); // space between two args is processed as 2 options by CommandLineToList ;)
  6. Fpr.Parameters.Add(tmpDBN);
  7. Fpr.Parameters.Add(Format('-r%s',[tmpSQL]));
  8. Fpr.Parameters.Add('--create-options');

Otherwise you can try CommandToList() to see how the command line that actually works has to be split...

BSaidus

  • Sr. Member
  • ****
  • Posts: 453
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: TProcess Syntaxe problems
« Reply #4 on: September 24, 2017, 04:05:59 pm »
So it was line 8 ?

Code: Pascal  [Select][+][-]
  1. Fpr.Parameters.Add(Format('--host=%s', [FHost]));
  2. Fpr.Parameters.Add(Format('--user=%s', [FUser]));
  3. Fpr.Parameters.Add(Format('--password=%s', [FPass]));
  4. Fpr.Parameters.Add(Format('--port=%d', [FPort]));
  5. Fpr.Parameters.Add('--database'); // space between two args is processed as 2 options by CommandLineToList ;)
  6. Fpr.Parameters.Add(tmpDBN);
  7. Fpr.Parameters.Add(Format('-r%s',[tmpSQL]));
  8. Fpr.Parameters.Add('--create-options');

Otherwise you can try CommandToList() to see how the command line that actually works has to be split...


YEEEEEEES !! you right !! Cool it works !!
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

guest58172

  • Guest
Re: TProcess Syntaxe problems
« Reply #5 on: September 24, 2017, 04:17:30 pm »
Yeah. I think i didn't express well myself in the first answer.

Prakash

  • Jr. Member
  • **
  • Posts: 59
Re: TProcess Syntaxe problems
« Reply #6 on: January 26, 2023, 05:50:02 am »
Code: Pascal  [Select][+][-]
  1.   Screen.Cursor := crHourGlass;
  2.   try
  3.     Fpr := TProcess.Create(application);
  4.  
  5.     Fpr.Parameters.Clear;
  6.     Fpr.Parameters.Add(Format('--host=%s', [sHost]));
  7.     Fpr.Parameters.Add(Format('--user=%s', [sUser]));
  8.     Fpr.Parameters.Add(Format('--password=%s', [sPwd]));
  9.     Fpr.Parameters.Add(Format('--port=%s', [sPort]));
  10.     Fpr.Parameters.Add('--database'); // space between two args is processed as 2 options by CommandLineToList ;)
  11.     Fpr.Parameters.Add(sDatabase);
  12.     Fpr.Parameters.Add(Format('-r%s',[sFilename]));
  13.     Fpr.Parameters.Add('--create-options');
  14.     Fpr.Executable := sExe;
  15.     Fpr.Options := Fpr.Options + [poWaitOnExit] + [poNoConsole];
  16.     Fpr.Execute;
  17.     //while Fpr.Running do Application.ProcessMessages;
  18.  
  19.   finally
  20.     Screen.Cursor:=crDefault;
  21.     //Fpr.Free;
  22.   end;
  23.  
  24.  

it is not executing . no response from the application .
what is the mistake in my code

dseligo

  • Hero Member
  • *****
  • Posts: 953
Re: TProcess Syntaxe problems
« Reply #7 on: January 26, 2023, 09:03:00 am »
it is not executing . no response from the application .
what is the mistake in my code

Your code works, I tried it with Lazarus 2.2.4 in Windows 11.
What is your OS and Lazarus version?

See here for executing external programs: https://wiki.freepascal.org/Executing_External_Programs#An_improved_example_.28but_not_correct_yet.29.
Here it says that in Linux you have to use full path to the executable: https://wiki.freepascal.org/TProcess.

I suggest you at least read output you get.

Add this after Fpr.Execute;:
Code: Pascal  [Select][+][-]
  1.     Fpr.Execute;
  2.  
  3.     ...
  4.  
  5.     AStringList := TStringList.Create;
  6.     AStringList.LoadFromStream(Fpr.Stderr);
  7.     AStringList.SaveToFile('output.txt'); // or ShowMessage(AStringList.Text);
  8.     AStringList.Free;

You'll have output in 'output.txt' file, so you can check what is wrong.

Edit: for reading out change options like that (add [poUsePipes]):
Code: Pascal  [Select][+][-]
  1.     Fpr.Options := Fpr.Options + [poWaitOnExit] + [poNoConsole] + [poUsePipes];
« Last Edit: January 26, 2023, 09:11:31 am by dseligo »

Prakash

  • Jr. Member
  • **
  • Posts: 59
Re: TProcess Syntaxe problems
« Reply #8 on: January 26, 2023, 11:52:13 am »
Thanks. It is working now.

Thaddy

  • Hero Member
  • *****
  • Posts: 12933
Re: TProcess Syntaxe problems
« Reply #9 on: January 26, 2023, 01:18:00 pm »
Other option:
Code: Pascal  [Select][+][-]
  1.    Fpr.CommandLine :=  Format('%s --host=%s --user=%s --password=%s  --port=%d --create-options --database %s -r%s', [FDump, FHost, FUser, FPass, FPort, tmpDBN, tmpSQL]);;
  2.    Fpr.RunCommandLoop;
  3.  
This needs 3.2.2 or higher.
See the manual for the exact syntax.
https://www.freepascal.org/docs-html/current/fcl/process/tprocess.runcommandloop.html
Last week I showed this in a complete example:
https://forum.lazarus.freepascal.org/index.php/topic,61841.msg466741.html#msg466741
« Last Edit: January 26, 2023, 01:22:09 pm by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

 

TinyPortal © 2005-2018