I created a Windows service with Delphi and used two method to install, start and stop.
Method 1
if i install this service using commandline
You have clashed with a bug in TService
implementation, see QC #79781. Delphi is not able to start the service if the service name if different from TService.Name
.
However, you can avoid this limitation by adjusting TService.Name
before the service is started. One good point to do this is the TService.OnCreate
event. You need to know the real name of the service, so you need to pass it as an argument to the service exe (adding it to the binpath
of the sc create
command).
Create the service:
sc create myservice1 binpath= "c:\MyService\ServiceApp.exe myservice1"
sc create myservice2 binpath= "c:\MyService\ServiceApp.exe myservice2"
Adjust the name:
procedure TMyService.ServiceCreate(Sender: TObject);
begin
if (System.ParamCount >= 1) and not CharInSet(ParamStr(1)[1], SwitchChars) then
Name := ParamStr(1);
end;
This is a somewhat rudimentary method of argument parsing, but it is ok as an example. If the first argument doesn't start with /
or -
, it assumes that it's the supplied name.
Remark:
Another limitation of TService
is that it can't create services (using /install
) with arguments in their command line, because it uses ParamStr(0)
as the binpath
.