Delphi Windows Services Command Line Arguments

I have a Deplhi-based Windows service that parses some command line arguments during installation. I want these arguments to be added to the services command line (ImagePath value in the registry) so that the service always starts with them.

How can i do this?

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

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

thanks

Update: Installation is done using >MyService.exe /install -some -arguments . These arguments are the ones I want to save on the command line.

Update: I found a solution by writing directly in the registry (see here ), but I still like a more elegant solution, for example, using some TService property or something like that. Thanks!

+6
delphi windows-services
source share
6 answers

Well, after some research, I abandoned the elegant approach and immediately sent directly to the registry.

To keep things simple, I did the following: I save the arguments that I wanted to pass in variables in my TService. Then I set the AfterInstall event to write directly to the registry (using the 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!

+8
source share

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

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

+4
source share

I just found out something amazing, maybe worthy to share.

You can specify options for Windows services in (at least) the following ways:

  • Enter it in the Service Manager GUI as "Startparameter".
  • Pass it as arguments: sc.exe YourService param1 param2
  • Enter it in the registry in the entry ImagePath = .. \ YourService.exe param1 param2

There are two ways to request application / service parameters in Delphi:

  • System.ParamCount / System.ParamStr: this way you get the parameters 3. above.
  • TService.ParamCount / TService.Param []: this way you get parameters 1. and 2. above.
+2
source share

You must use the SCM API to properly install and configure the service. SC.EXE is the command line interface for the SCM API. You should not manipulate the registry directly in this situation, it works, but also depends on what permission the service account has. In any case, I wonder why command line arguments are needed if you insert them into the code - usually these arguments are installed outside the service (by the installer, configurator, etc.) to change the behavior of the service.

+2
source share

I donโ€™t think that you can force the service to start from them, but if you save these parameters in the registry, you can change the program for future starts, always go and grab them. (i.e. if ParamCount = 0, then get the parameters from the registry)

0
source share

Pablo, I had the same question, he did not find anything and went along the path of editing the registry. In addition, I use Custom Actions also to create additional installation and copy directories for files (in C #). Your suggestion was helpful, and I stopped looking for an โ€œelegantโ€ way. thanks

0
source share

All Articles