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!
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With