如何在安装而不是每次启动时一劳永逸地将参数传递给 Windows 服务 [英] How to pass a parameter to a windows service once and for all at install instead of each start

查看:19
本文介绍了如何在安装而不是每次启动时一劳永逸地将参数传递给 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

MyService -option /install

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

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

更新

各个服务参数存储在注册表中的注册表项 HKLMSYSTEMCurrentControlSetServicesParameters 处.我不确定参数是如何传递给服务的.我相信当 SCM 调用 StartService 它将它们传递给 ServiceMain 回调.

The individual service parameters are stored in the the registry at the key HKLMSYSTEMCurrentControlSetServices<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天全站免登陆