Recent

Author Topic: Force parameters of my application  (Read 1185 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8379
Re: Force parameters of my application
« Reply #15 on: March 19, 2025, 02:02:54 pm »
To everything that's been said here already, I would add that it is of course easy enough to redefine ParamStr() and ParamCount() so that they refer e.g. the content of a StringList which is populated first by defaults and then by using System.ParamStr() and System.ParamCount().

OP has not, to his discredit, told us whether he's using Lazarus or straight FPC, or (as a lesser concern) whether it's Windows, Linux etc. /If/ he's using Lazarus then there's undoubtedly ways of overriding the Application object which provides a thin wrapper around ParamStr() etc., but quite frankly I don't think that would be worth the effort.

I've done this sort of thing in the past where I wanted to pull a sequence of options (used in the unix sense) and then refer to the parameters that followed them sequentially.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: Force parameters of my application
« Reply #16 on: March 19, 2025, 02:25:08 pm »
OP has not, to his discredit, told us whether he's using Lazarus or straight FPC, or (as a lesser concern) whether it's Windows, Linux etc. /

MarkMLl
Well, from the very first post:
Quote
application1.exe --winner
should give a firm hint, that it's windows.
But considering his Screenname ("...TUX").... *shrug*
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

Ericktux

  • Sr. Member
  • ****
  • Posts: 377
    • ericksystem software
Re: Force parameters of my application
« Reply #17 on: March 19, 2025, 03:17:38 pm »
I found this solution, so far it works well on Windows and Linux.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms, unit1, process
  14.   { you can add units after this };
  15.  
  16. {$R *.res}
  17.  
  18. procedure RelaunchWithParam;
  19. var
  20.   AProcess: TProcess;
  21. begin
  22.   AProcess := TProcess.Create(nil);
  23.   try
  24.     AProcess.Executable := ParamStr(0);  // Ruta del ejecutable
  25.     AProcess.Parameters.Add('--winner'); // Agregar parámetro
  26.     //AProcess.Options := [poWaitOnExit];      // No esperar a que termine
  27.     AProcess.Execute;                    // Ejecutar nueva instancia
  28.   finally
  29.     AProcess.Free;
  30.   end;
  31.   Halt;  // Detener la instancia original
  32. end;
  33.  
  34. begin
  35.     // Si no hay parámetros, relanzamos con "--winner"
  36.   if ParamCount = 0 then
  37.     RelaunchWithParam;
  38.  
  39.   RequireDerivedFormResource:=True;
  40.   Application.Scaled:=True;
  41.   Application.Initialize;
  42.   Application.CreateForm(TForm1, Form1);
  43.   Application.Run;
  44. end.


I love desktop software
https://www.ericksystem.com

 

TinyPortal © 2005-2018