Recent

Author Topic: [SOLVED] TProcess Execute fails/not running  (Read 1192 times)

DanishMale

  • Jr. Member
  • **
  • Posts: 94
[SOLVED] TProcess Execute fails/not running
« on: March 04, 2025, 06:06:48 pm »
Hi

I have created a daemon app which on certain times should execute external apps.

The code for starting the process is called like this:

StartExtApp(AppPath+'ExternalApp.exe');  or  StartExtApp(AppPath+'ExternalApp.exe','Parameters');

Code: Pascal  [Select][+][-]
  1. procedure StartExtApp(ExtAppName: String; Arguments: String = '');
  2. var
  3.   ExtApp: TProcess;
  4.   ReturnedString: TStringList;
  5.   ParamArgs: Array Of String;
  6.   ParamNo: Integer;
  7. begin
  8.   try
  9.     ExtApp := TProcess.Create(nil);
  10.     ExtApp.Executable := '"'+ExtAppName+'"';
  11.     ExtApp.Parameters.Clear;
  12.     if Arguments <> '' then
  13.     begin
  14.       ParamArgs := SplitString(Arguments,'\\');
  15.       for ParamNo := Low(ParamArgs) to High(ParamArgs) do
  16.         ExtApp.Parameters.Add('"'+ParamArgs[ParamNo]+'"');
  17.     end;
  18.     ExtApp.Options := [poWaitOnExit, poUsePipes]; //,poNoConsole];
  19.     ExtApp.Execute;  <<<<<<<<< EXITS APP (NO HANGING) HERE NO ERROR NO NOTHING....
  20.     ReturnedString := TStringList.Create;
  21.     ReturnedString.LoadFromStream(ExtApp.Output);
  22.     WriteLog(ReturnedString.Text,LOG_INFO);
  23.     ExtApp.Free;
  24.     ReturnedString.Free;
  25.   except
  26.     on E: Exception do
  27.     begin
  28.       WriteLog(ReturnedString.Text,LOG_INFO);
  29.       WriteLog(E.Message,LOG_INFO);
  30.       ExtApp.Free;
  31.       ReturnedString.Free;
  32.     end;
  33.   end;
  34. end;
  35.  

Sample from Lazarus WIKI has been used as base for the TProcess procedure.

Just wonder how to solve this issue...  Thx in advance for any help.
« Last Edit: March 05, 2025, 09:37:52 pm by DanishMale »
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12653
  • FPC developer.
Re: TProcess Execute fails/not running
« Reply #1 on: March 04, 2025, 06:37:14 pm »
And the problem is what exactly? If it is hanging, read the large output section in that wiki.

DanishMale

  • Jr. Member
  • **
  • Posts: 94
Re: TProcess Execute fails/not running
« Reply #2 on: March 05, 2025, 03:34:37 am »
And the problem is what exactly? If it is hanging, read the large output section in that wiki.

ExtApp.Execute;  <<<<<<<<< EXITS APP (NO HANGING) HERE NO ERROR NO NOTHING....
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12653
  • FPC developer.
Re: TProcess Execute fails/not running
« Reply #3 on: March 05, 2025, 09:05:52 am »
Code: Delphi  [Select][+][-]
  1.  
  2.  ExtApp.Executable := '"'+ExtAppName+'"';
  3.  ...
  4.     ExtApp.Parameters.Add('"'+ParamArgs[ParamNo]+'"');
  5.  
  6.  

Remove the quoting from these. If it is not a commandline, but separate strings, no quoting is needed (or even: it is not allowed)

Zvoni

  • Hero Member
  • *****
  • Posts: 3270
Re: TProcess Execute fails/not running
« Reply #4 on: March 05, 2025, 09:21:23 am »
Code: Delphi  [Select][+][-]
  1.  
  2.  ExtApp.Executable := '"'+ExtAppName+'"';
  3.  ...
  4.     ExtApp.Parameters.Add('"'+ParamArgs[ParamNo]+'"');
  5.  
  6.  

Remove the quoting from these. If it is not a commandline, but separate strings, no quoting is needed (or even: it is not allowed)
Marco,
he's passing the full path of the executable
Quote
StartExtApp(AppPath+'ExternalApp.exe');  or  StartExtApp(AppPath+'ExternalApp.exe','Parameters');
You need the quotes if, e.g. you have spaces in your paths.

My question is: Is the full path well formed? As in: Maybe there is a DirectoryDelimiter missing between Path and exe
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12653
  • FPC developer.
Re: TProcess Execute fails/not running
« Reply #5 on: March 05, 2025, 09:29:31 am »
You need the quotes if, e.g. you have spaces in your paths.

Only if you have a commandline. If it is a single string, there is no separation needed that can stumble on the spaces. I repeat, in this case no quotes are necessary.

DanishMale

  • Jr. Member
  • **
  • Posts: 94
Re: TProcess Execute fails/not running
« Reply #6 on: March 05, 2025, 11:57:56 am »
AppPath is defined like this

         AppPath := ExtractFilePath(ParamStr(0))+'\';

And there are spaces in both AppPath and in some of the Parameter, this is the reason why the passed parameters are seperated by \\
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

cdbc

  • Hero Member
  • *****
  • Posts: 2625
    • http://www.cdbc.dk
Re: TProcess Execute fails/not running
« Reply #7 on: March 05, 2025, 12:09:46 pm »
Hi
Just for the record:
Code: Pascal  [Select][+][-]
  1. ExtractFilePath();
returns the path WITH trailing pathdelimiter...!
So there's no need to append an extra...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3270
Re: TProcess Execute fails/not running
« Reply #8 on: March 05, 2025, 12:14:07 pm »
hmmm... ok, i trust marco to know what's he's talking about.

OTOH: TS stated it's a daemon (Service on Windows).
Under which User-Context is this daemon supposed to run?
Maybe something's happening there....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: TProcess Execute fails/not running
« Reply #9 on: March 05, 2025, 12:41:56 pm »
The big issue is the added ", which should not be there, unless paths containing spaces. There is no GUI interaction, so there should be no problem running that as a service. IF there is any GUI interaction code within the daemon and the platform is Windows 10 or higher than it won't work anyway:
It will need a controller app to interface with the Daemon/service.
But we only have a snippet.

I think Marcov is right, but just in the case the OP does not have ANY gui code in his app. Logging is OK. In older Windows, like 7 or even XP, such code used to work.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12653
  • FPC developer.
Re: TProcess Execute fails/not running
« Reply #10 on: March 05, 2025, 12:47:59 pm »
Quote
OTOH: TS stated it's a daemon (Service on Windows).
Under which User-Context is this daemon supposed to run?
Maybe something's happening there....

That could be. Launching visible applications from non visible processes might be prohibited, or require lowering of permissions. To see if that is happing first test in standalone app.

As far as paths go, it is best to use includetrailingpathdelimiter instead of hardcoding backslash. Simply because it is not needed to hardcode it. Platform concerns, but more importantly avoid double path delimiters where some APIs might stumble on.

 It would become something like this:

Code: Pascal  [Select][+][-]
  1.  AppPath := includetrailingpathdelimiter(ExtractFilePath(ParamStr(0)));
  2.  

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: TProcess Execute fails/not running
« Reply #11 on: March 05, 2025, 01:24:20 pm »
Always a peculiar topic trying to run before able to walk  :)

caller
Code: Pascal  [Select][+][-]
  1. program caller;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   classes, sysutils, process;
  7.  
  8. var
  9.   ExtAppName   : string = 'callee';
  10.   Args         : array of string = ('this is', 'a parameter', 'and', 'another one');
  11.   OutPutResult : string;
  12.   ExitStatus   : integer;
  13.   returnvalue  : integer;
  14.   log          : string;
  15.   logger       : TStringList;
  16. begin
  17.   logger := TStringList.Create;
  18.  
  19.   returnvalue := RunCommandInDir('.', ExtAppName, Args, OutPutResult, ExitStatus, [poWaitOnExit, poUsePipes]);
  20.   log := '';
  21.   WriteStr(log, log, 'Application  = ', ExtAppName, LineEnding);
  22.   WriteStr(log, log, 'Arguments    = ', ''.Join(',', Args), LineEnding);
  23.   WriteStr(log, log, 'ExitStatus   = ', ExitStatus, LineEnding);
  24.   WriteStr(log, log, 'ReturnValue  = ', ReturnValue, LineEnding);
  25.   WriteStr(log, log, 'OutputResult : ', LineEnding, OutputResult);
  26.   Writeln(log);
  27.   Logger.Add(log);
  28.  
  29.   logger.SaveToFile('caller.log');
  30.   logger.Free;
  31. end.
  32.  

callee
Code: Pascal  [Select][+][-]
  1. program callee;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   classes, sysutils;
  7.  
  8. var
  9.   logger : TStringList;
  10.   s      : string;
  11.   n      : integer;
  12. begin
  13.   logger := TStringList.Create;
  14.  
  15.   writeln('caller invoked callee with parameters:');
  16.  
  17.   for n := 1 to ParamCount do
  18.   begin
  19.     WriteStr(s, 'ParamStr(', n, ') = ', ParamStr(n));
  20.     logger.Add(s);
  21.     Writeln(s);
  22.   end;
  23.  
  24.   logger.SaveToFile('callee.log');
  25.  
  26.   logger.Free;
  27. end.
  28.  

callee.log
Code: [Select]
ParamStr(1) = this is
ParamStr(2) = a parameter
ParamStr(3) = and
ParamStr(4) = another one

caller.log
Code: [Select]
Application  = callee
Arguments    = this is,a parameter,and,another one
ExitStatus   = 0
ReturnValue  = 0
OutputResult :
caller invoked callee with parameters:
ParamStr(1) = this is
ParamStr(2) = a parameter
ParamStr(3) = and
ParamStr(4) = another one

So, no doubt. Marco is right.
Today is tomorrow's yesterday.

DanishMale

  • Jr. Member
  • **
  • Posts: 94
Re: TProcess Execute fails/not running
« Reply #12 on: March 05, 2025, 06:00:32 pm »
The problem was solved after I uninstalled daemon package and reinstalled the package. Now it works without quotes as expected ..... Some how there has been an error in the package... but solved now :)

Thx all for their input and valued information and knowledge
« Last Edit: March 05, 2025, 09:40:57 pm by DanishMale »
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

 

TinyPortal © 2005-2018