如何传递一个参数到一个Windows服务一次和所有在安装而不是每个开始 [英] How to pass a parameter to a windows service once and for all at install instead of each start

查看:536
本文介绍了如何传递一个参数到一个Windows服务一次和所有在安装而不是每个开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  MyService -option 

到目前为止,当我们想使用参数启动服务时,我们可以从服务属性对话框启动参数框)或使用命令

  sc start MyService -option 
pre>

我们想要的是使用此参数永久安装服务的方法,以便用户只需要开始/停止它,而不必每次都设置参数



BTW,在ImagePath注册表项中添加参数不起作用,安装方式如下:

  MyService -option / install 

更新:感谢您迄今为止的答案,帮助我改进了问题。

我想实现是在服务级别本身(类似于属性)中设置参数,以便在同一可执行文件中存在多个服务的情况下。 binpath配置选项只是更新注册表中的ImagePath条目。这不能是服务特定的。

解决方案

  sc config MyService binPath = MyService.exe -option 

更新



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


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

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:

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.

解决方案

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 believe SCM reads these values then when it calls StartService it passes them to the ServiceMain callback.

这篇关于如何传递一个参数到一个Windows服务一次和所有在安装而不是每个开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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