Service does not start

☆樱花仙子☆ 提交于 2019-12-04 12:05:17

问题


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:


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.



来源:https://stackoverflow.com/questions/18599201/service-does-not-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!