Recent

Author Topic: Executing external program  (Read 17377 times)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Executing external program
« Reply #15 on: July 26, 2013, 07:45:35 am »
more generic for this, a function :
Code: [Select]
function execCmd(cmd: string; wait : boolean) : integer;
// J.P 2013
var AProcess : TProcess;
var exitcode : integer;
begin
result := -1;
AProcess := TProcess.Create(nil);
try
{$IFDEF WIN32}
AProcess.Executable := 'cmd.exe' ;
AProcess.Parameters.DelimitedText :=  '/c "' + cmd + '"';
{$ENDIF}
{$IFDEF LINUX}
AProcess.Executable := 'bash' ;
AProcess.Parameters.DelimitedText :=  '-c "' + cmd + '"';
{$ENDIF}
if wait then AProcess.Options := AProcess.Options + [poWaitOnExit]; //  wait for the end of the process
AProcess.ShowWindow := swoHide;
AProcess.Execute;
Except
on E: Exception do
      ShowMessage('Error with cmd  : ' + E.message);  //Show Exception
end;
result := AProcess.ExitStatus;
Aprocess.Free;
end;

example to call the function :
Code: [Select]
var exitcode : integer;
begin
{$IFDEF WIN32}
execCmd('notepad.exe',false);
exitcode :=  execCmd('sort<f:\testsort.txt >f:\resultu.txt',true);
{$ENDIF}
{$IFDEF LINUX}
exitcode :=  execCmd('sort</home/user/testsort.txt >/home/user/result.txt',true);
{$ENDIF}
if (exitcode <> 0 ) then ShowMessage('Error exit code = ' + inttostr(exitcode));
end; 

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #16 on: July 28, 2013, 12:10:50 pm »
If I write to the console
Code: [Select]
/home/alex/Рабочий\ стол/project/myapp param1 param2 < /home/alex/Рабочий\ стол/data/in.file
everything works. But
Code: [Select]
AProcess.Executable := 'bash';
AProcess.Parameters.Text :=
  '-c "/home/alex/Рабочий\ стол/project/myapp param1 param2 < /home/alex/Рабочий\ стол/data/in.file"';
AProcess.Options := AProcess.Options + [poWaitOnExit];
AProcess.ShowWindow := swoHIDE;
AProcess.Execute;
has no effect. What is wrong?

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Executing external program
« Reply #17 on: July 28, 2013, 12:27:40 pm »
hello,

 :o  --> try 
Code: [Select]
AProcess.Parameters.DelimitedText
in place of ;
Code: [Select]
AProcess.Parameters.Text
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #18 on: July 28, 2013, 02:52:50 pm »
hello,

 :o  --> try 
Code: [Select]
AProcess.Parameters.DelimitedText
in place of ;
Code: [Select]
AProcess.Parameters.Text
Oops, this is my typo. Thanks

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #19 on: July 29, 2013, 05:52:26 am »
Code: [Select]
function execCmd(cmd: string; wait : boolean) : integer;
...
{$IFDEF WIN32}
AProcess.Executable := 'cmd.exe' ;
AProcess.Parameters.DelimitedText :=  '/c "' + cmd + '"';
{$ENDIF}
...
If the variable 'cmd' contains a spaces ('C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe param1 param2 < in.file'), then the launch will not work. Adding double quotes (C:\"Documents and Settings"\...) does not solve the problem.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Executing external program
« Reply #20 on: July 29, 2013, 06:26:35 am »
the ending double quote is in a wrong place in your case :

the command is  :
Code: [Select]
cmd /c "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe param1 param2 < in.file"
must be :
Code: [Select]
[code]cmd /c "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe" param1 param2 < in.file[/code]

you can also use  this for the variable cmd in your case : 

Code: [Select]
' "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe" param1 param2 < in.file'
« Last Edit: July 29, 2013, 06:32:47 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #21 on: July 29, 2013, 06:53:53 am »
Code: [Select]
  AProcess := TProcess.Create(nil);
  try
    AProcess.Executable := 'cmd.exe';
    AProcess.Parameters.DelimitedText :=
      '/c "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe" param1 param2 < "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\in.file"';
    AProcess.Options := AProcess.Options + [poWaitOnExit];
    AProcess.ShowWindow := swoHide;
    AProcess.Execute;
  finally
     AProcess.Free;
  end; 
No effect

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Executing external program
« Reply #22 on: July 29, 2013, 07:16:05 am »
You can't use spacebar as delimiter (did you even set so?), because second parameter contains spaces in path. You may use " " but it's Lazarus that reads, and doesn't understand it. By my assumption the parameters for TProcess are separated by line change, not space. So does this work?
Code: [Select]
  AProcess := TProcess.Create(nil);
  try
    AProcess.Executable := 'cmd.exe';
    AProcess.Parameters.Add('/c');
    AProcess.Parameters.Add('"C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe"');
    AProcess.Parameters.Add('param1');
    AProcess.Parameters.Add('param2');
    //AProcess.Parameters.Add('<'); // maybe works without space, so combine with next parameter?
    AProcess.Parameters.Add('<"C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\in.file"');
    AProcess.Options := AProcess.Options + [poWaitOnExit];
    AProcess.ShowWindow := swoHide;
    AProcess.Execute;
  finally
     AProcess.Free;
  end;
Then i there is also TUTF8Process in unit UTF8Process.pas. Might be relevant or not, because your path contains non A..Z.

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #23 on: July 29, 2013, 08:22:23 am »
Code: [Select]
  try
    AProcess.Executable := 'cmd.exe';
    AProcess.Parameters.Add('/c');
    AProcess.Parameters.Add('"C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe"');
...
Code: [Select]
AProcess.Parameters.Add('myapp.exe'); -- ok
AProcess.Parameters.Add('"C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe"'); -- fail
TProcessUTF8 has not changed the situation

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Executing external program
« Reply #24 on: July 30, 2013, 12:29:50 am »
Quote
TProcessUTF8 has not changed the situation

Are you sure ? because for me this code doesn't work :
Code: [Select]
var AProcess : TProcess;
begin
AProcess := TProcess.Create(nil);
AProcess.Executable := 'F:\Téléchargements\led.exe';
AProcess.Options := AProcess.Options + [poUsePipes];
AProcess.ShowWindow := swoSHOW;
AProcess.Execute;

Error 2  File not found

but this one works ( add UTF8Process in the "uses" )  :
Code: [Select]
var AProcessUTF8 : TProcessUTF8;
begin
AProcessUTF8 := TProcessUTF8.Create(nil);
AProcessUTF8.Executable := 'F:\Téléchargements\led.exe';
AProcessUTF8.Options := AProcessUTF8.Options + [poUsePipes];
AProcessUTF8.ShowWindow := swoSHOW;
AProcessUTF8.Execute;
my code page is 850

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #25 on: July 30, 2013, 05:30:40 am »
Code works:
Code: [Select]
var
  AProcess: TProcessUTF8;
begin
  AProcess := TProcessUTF8.Create(nil);
  try
    AProcess.Executable := 'cmd.exe';
    AProcess.Parameters.Add('/c');
    AProcess.Parameters.Add('"myapp.exe"');
    AProcess.Parameters.Add('param1');
    AProcess.Parameters.Add('param2');
    AProcess.Parameters.Add('<in.file');

    AProcess.Options := AProcess.Options + [poWaitOnExit];
    AProcess.ShowWindow := swoHide;
    AProcess.Execute;
  finally
     AProcess.Free;
  end;
Code doesn't work:
Code: [Select]
var
  AProcess: TProcessUTF8;
begin
  AProcess := TProcessUTF8.Create(nil);
  try
    AProcess.Executable := 'cmd.exe';
    AProcess.Parameters.Add('/c');
    AProcess.Parameters.Add('"C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe"');
    AProcess.Parameters.Add('param1');
    AProcess.Parameters.Add('param2');
    AProcess.Parameters.Add('<in.file');

    AProcess.Options := AProcess.Options + [poWaitOnExit];
    AProcess.ShowWindow := swoHide;
    AProcess.Execute;
  finally
     AProcess.Free;
  end;

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: Executing external program
« Reply #26 on: July 30, 2013, 05:44:38 am »
try this :

Code: [Select]
var
  AProcess: TProcessUTF8;
begin
  AProcess := TProcessUTF8.Create(nil);
  try
    AProcess.Commandline := 'cmd.exe /c "C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\myapp.exe" param1 param2 < in.file';
    AProcess.Options := AProcess.Options + [poWaitOnExit];
    AProcess.ShowWindow := swoHide;
    AProcess.Execute;
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Sanchoyzer

  • Jr. Member
  • **
  • Posts: 65
Re: Executing external program
« Reply #27 on: July 30, 2013, 06:20:09 am »
Something strange is going on...

It works
Code: [Select]
var
  AProcess: TProcessUTF8;
   prefix: string;
begin
  AProcess := TProcessUTF8.Create(nil);
  try
    prefix := 'C:\Documents and Settings\alex\Рабочий стол\test_prog_gui\';
    AProcess.Commandline := Format('cmd.exe /c "%s" %s param2 < %s',
        [prefix + 'myapp.exe', 'param1', 'data\in.file']);
...

It works
Code: [Select]
    AProcess.Commandline := Format('cmd.exe /c %s %s param2 < "%s"',
        ['myapp.exe', 'param1', prefix + 'data\in.file']);

It does not works
Code: [Select]
    AProcess.Commandline := Format('cmd.exe /c "%s" %s param2 < "%s"',
        [prefix + 'myapp.exe', 'param1', prefix + 'data\in.file']);

 

TinyPortal © 2005-2018