服务不启动 [英] Service does not start

查看:199
本文介绍了服务不启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





方法1

我使用Delphi创建了一个Windows服务,并使用两种方法来安装,启动和停止。 / strong>



如果我使用命令行安装此服务

  C:\MyService\ServiceApp.exe /安装

它已成功安装我可以在服务控制台中启动和停止。



方法2



但是如果我使用sc
例如使用不同的名称安装相同的服务

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

我看到它已经安装,但是我无法使用服务控制台启动服务以及

  sc start myservice 

当我使用SC进行查询时,结果如下

  C:\Windows\system32> sc query myservice 

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

到目前为止,我正在使用/安装,但我想要使用不同的名称多次安装相同的服务,我有这个想法从这个帖子使用。 (如何安装Windows从命令行指定名称和描述的服务
可以解释/ Install和SC之间的行为差​​异?

解决方案

您已经在 TService 实现中发生了一个错误,请参阅 QC#79781 。如果服务名称与 TService.Name 不同,则Delphi无法启动服务。



但是,您可以在开始服务之前调整 TService.Name 来避免这种限制。一个很好的做法是: TService.OnCreate 事件。您需要知道服务的真实名称,因此您需要将其作为参数传递给服务exe(将其添加到 binpath > sc创建命令)



创建服务

  sc create myservice1 binpath =c:\MyService\ServiceApp.exe myservice1
sc create myservice2 binpath =c:\MyService\ServiceApp。 exe myservice2

调整名称:

  procedure TMyService.ServiceCreate(Sender:TObject); 
begin
if(System.ParamCount> = 1)而不是CharInSet(ParamStr(1)[1],SwitchChars)然后
名称:= ParamStr(1);
结束

这是一个基本的参数解析方法,但它是一个例子。如果第一个参数不以 / - 开头,那么它假定它是提供的名称。 >

备注:



TService 是不能使用命令行中的参数来创建服务(使用 / install ),因为它使用 ParamStr(0)作为 binpath


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?

解决方案

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.

这篇关于服务不启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆