如何在安装而不是每次启动时一劳永逸地将参数传递给 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1488851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:46:13  来源:igfitidea点击:

How to pass a parameter to a windows service once and for all at install instead of each start

windowscommand-lineparametersserviceinstall

提问by Fran?ois

We have a Windows Service application that can accept command line parameters like:

我们有一个可以接受命令行参数的 Windows 服务应用程序,例如:

MyService -option 

So far, when we want to start the service with a parameter, we either do it manually from the Service Properties dialog (in the Start parameters box) or with the command

到目前为止,当我们想用参数启动服务时,我们要么从服务属性对话框(在启动参数框中)手动执行,要么使用命令

sc start MyService -option  

What we would like is a way to install the service "permanently" with this parameter, so that the users would just have to start/stop it without having to set the parameter each time.

我们想要的是一种使用此参数“永久”安装服务的方法,这样用户只需启动/停止它,而不必每次都设置该参数

BTW, adding the parameter in the ImagePath registry entry doesn't work, neither does installing like this:

顺便说一句,在 ImagePath 注册表项中添加参数不起作用,像这样安装也不起作用:

MyService -option /install

Updated: Thank you for the answers so far which help me refine the question.
What I'd like to achieve is to set the parameter at the Service level itself(like with the properties) in case there are more than 1 service in the same executable. The binpath config option is merely updating the ImagePath entry in the registry. That cannot be service specific.

更新:感谢您到目前为止的答案,它们帮助我完善了问题。
我想要实现的是在服务级别本身设置参数(就像使用属性一样),以防同一个可执行文件中有 1 个以上的服务。binpath 配置选项只是更新注册表中的 ImagePath 条目。这不能是特定于服务的。

回答by Remus Rusanu

sc config MyService binPath= MyService.exe -option

Update

更新

The individual service parameters are stored in the the registry at the key HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters. I'm not sure though how the parameters are passed to the service. I believeSCM reads these values then when it calls StartServiceit passes them to the ServiceMaincallback.

各个服务参数存储在注册表中的键 处HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters。我不确定参数是如何传递给服务的。我相信SCM 会在调用时读取这些值,然后StartService将它们传递给ServiceMain回调。

回答by Raj More

How about putting the parameter in a config file?

如何将参数放在配置文件中?

回答by Rosberg Linhares

According to the ServiceBase.OnStartdocumentation:

根据ServiceBase.OnStart文档:

The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();.

在控制台中输入的参数不会被保存;当从控制面板启动服务时,它们会一次性传递给服务。服务自动启动时必须存在的参数可以放在服务注册表项 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\) 的 ImagePath 字符串值中。您可以使用 GetCommandLineArgs 方法从注册表中获取参数,例如:string[] imagePathArgs = Environment.GetCommandLineArgs();。

回答by virgored

Powershell can do this but you have to use .Net to achieve it.

Powershell 可以做到这一点,但你必须使用 .Net 来实现它。

new-Object System.ServiceProcess.ServiceController("$ServiceName",$ComputerName)).Start("$Parameter")

回答by Dustin

Arguments passed on the command-line via ImagePath are accessible in main() or via GetCommandLine(). You could install with command-line args and then in your ServiceMain, check to see if any arguments were passed in the lpszArgs parameter. If not, call GetCommandLine and see if any were passed that way.

通过 ImagePath 在命令行上传递的参数可以在 main() 或 GetCommandLine() 中访问。您可以使用命令行参数进行安装,然后在您的 ServiceMain 中检查是否在 lpszArgs 参数中传递了任何参数。如果没有,请调用 GetCommandLine 并查看是否通过这种方式传递。

回答by timothy

If there is more than one service with the same executable then you would be installing them with different service names. You could refer to the service name instead of the parameters.

如果有多个服务具有相同的可执行文件,那么您将使用不同的服务名称安装它们。您可以参考服务名称而不是参数。

To get the service name you can use this How can a Windows Service determine its ServiceName?

要获取服务名称,您可以使用此 Windows 服务如何确定其服务名称?

回答by Tejas Bagade

Use the SC (service control) command, it gives you a lot more options than just start & stop.

使用 SC(服务控制)命令,它为您提供了比启动和停止更多的选项。

DESCRIPTION:
          SC is a command line program used for communicating with the
          NT Service Controller and services.
  USAGE:
      sc <server> [command] [service name]  ...

      The option <server> has the form "\ServerName"
      Further, help on commands can be obtained by typing: "sc [command]"
      Commands:
        query-----------Queries the status for a service, or
                        enumerates the status for types of services.
        queryex---------Queries the extended status for a service, or
                        enumerates the status for types of services.
        start-----------Starts a service.
        pause-----------Sends a PAUSE control request to a service.
        interrogate-----Sends an INTERROGATE control request to a service.
        continue--------Sends a CONTINUE control request to a service.
        stop------------Sends a STOP request to a service.
        config----------Changes the configuration of a service (persistent).
        description-----Changes the description of a service.
        failure---------Changes the actions taken by a service upon failure.
        qc--------------Queries the configuration information for a service.
        qdescription----Queries the description for a service.
        qfailure--------Queries the actions taken by a service upon failure.
        delete----------Deletes a service (from the registry).
        create----------Creates a service. (adds it to the registry).
        control---------Sends a control to a service.
        sdshow----------Displays a service's security descriptor.
        sdset-----------Sets a service's security descriptor.
        GetDisplayName--Gets the DisplayName for a service.
        GetKeyName------Gets the ServiceKeyName for a service.
        EnumDepend------Enumerates Service Dependencies.

      The following commands don't require a service name:
      sc <server> <command> <option>
        boot------------(ok | bad) Indicates whether the last boot should
                        be saved as the last-known-good boot configuration
        Lock------------Locks the Service Database
        QueryLock-------Queries the LockStatus for the SCManager Database
  EXAMPLE:
          sc start MyService

回答by gatsby

The only thing that worked for me was to add the parameter to the ImagePath on the registry like the following image : enter image description here

唯一对我有用的是将参数添加到注册表上的 ImagePath 中,如下图所示: 在此处输入图片说明