Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Windows Services command line arguments

I have a Deplhi based Windows Service that, on installation, parses some command line arguments. I want those arguments to be added to the services command line (ImagePath value on the registry) so that the service is always started with them.

How can I accomplish this?

I want the regedit look like this:
at registry key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\MyService

  • ImagePath = C:\Path\to\my\service.exe -some -arguments

Thanks

Update: The installation is done with >MyService.exe /install -some -arguments. Those arguments are the ones I want to persist in the command line.

Update: I found a solution by writing directly to the registry (see here), but I'd still like a more elegant solution, like using some TService property or something of that sort. Thanks!

like image 479
Pablo Venturino Avatar asked Dec 26 '08 13:12

Pablo Venturino


2 Answers

Ok, after some research, I gave up on an elegant approach, and took the straight-forward path of writing directly to the registry.

To make things simple, I did this: I store the arguments I wanted to pass in variables on my TService. Then, I set the AfterInstall event to write directly into the registry (using a TRegistry object) the exact command line I wanted.

uses Registry;
procedure MyService.AfterInstall(Sender: TObject) ;
var
  reg:TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := 'HKEY_LOCAL_MACHINE';
    if reg.OpenKey('SYSTEM\CurrentControlSet\Services\MyService', True) then
    begin
      reg.WriteExtendString ('ImagePath', ParamStr(0) + ' -some -arguments') ;
      reg.CloseKey;
    end;
  finally
    reg.Free;
  end;
end;

Not the elegant solution I was looking for, but it does the job.

Thanks for the other answers through!

like image 171
Pablo Venturino Avatar answered Sep 28 '22 05:09

Pablo Venturino


Service arguments can be passed in the lpBinaryPathName argument to the CreateService function. In Delphi's TService, this is called inside TServiceApplication.RegisterServices.InstallService, which you cannot override (easily).

Therefore, I suspect the easiest way to do this is going to be to handle the TService.AfterInstall event and update the registry yourself via ChangeServiceConfig.

like image 29
Craig Stuntz Avatar answered Sep 28 '22 03:09

Craig Stuntz