Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service does not start

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?

like image 670
Girish Avatar asked Sep 03 '13 18:09

Girish


1 Answers

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.

like image 149
JRL Avatar answered Oct 01 '22 19:10

JRL