10
votes

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

C:\MyService\ServiceApp.exe /Install

it installed successfully and i can start and stop too in the service console.

Method 2

but if i install the same service with different name using sc e.g.

C:\Windows\system32>sc create myservice binpath= c:\MyService\ServiceApp.exe

I see it is installed but i can not start the service using service console as well as with

sc start myservice

when i do query using SC , result are as follows

C:\Windows\system32>sc query myservice

SERVICE_NAME: myservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0

up till now i was using /Install but i want to install same service multiple times with different names, I got this idea of using from this post. (How to install a windows service from command line specifying name and description?) can anybody explain difference of behavior between /Install and SC?

1
If you can show us the code you have which installs it and also which starts it, we might be able to help you.Jerry Dodge
To install: sc create myservice binpath= c:\serviceapp\ServiceApp.exe to start: sc start myservice or simply start from service consoleGirish
Please edit your question to include this critical information, not the comments.Jerry Dodge
hi jerry,In question i already how i installed the service in method 2Girish
I can verify the problem on my box.Sertac Akyuz

1 Answers

17
votes

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.