使用sc.exe创建服务时如何传递上下文参数?

183

使用以下方式创建Windows服务:

sc create ServiceName binPath= "the path"

如何将参数传递给Installer类的Context.Parameters集合?

根据我对sc.exe文档的理解,这些参数只能通过binPath的末尾传递,但我没有找到示例或成功执行此操作。


快速浏览注册表中的“服务”键,可以发现任何必要的参数都包含在ImagePath值中,因此您的 binPath= "c:\abc\def.exe /Param1=ghi" 看起来是正确的想法。反斜杠需要转义吗(即 "c:\abc\...")?最坏的情况下,如果SC.EXE无法完成操作,您可以直接编辑注册表值。 - ewall
1
我放弃了 sc.exe,并改用 installutil.exe,命令如下:Installutil.exe /ServiceName=”服务名称” /targetdir=”C:\安装目录\” /PackageRoot=”包根路径” - sympatric greg
我使用了Installutil.exe,对于旧技术,我使用来自Windows XP/2003资源套件的Instsrv.exe。 - Gary Kindel
14个回答

0
如果您已尝试了上述所有方法,但仍无法将 args 传递给您的服务,且您的服务是用 C/C++ 编写的,则可能存在以下问题:当您通过 "sc start arg1 arg2..." 启动服务时,SC 直接使用这些参数调用您的服务的 ServiceMain 函数。但是,当 Windows 启动您的服务(例如,在启动时),它会调用您的服务的主函数 (_tmain),并从注册表的 "binPath" 获取参数。

0

确保在binPath值的开头和结尾都有引号。

sc create Stuff binPath= "C:\Windows\system32\stuff.exe" DisplayName= "Stuff Update Service" start= auto

sc description Stuff "service to update stuff"

sc query Stuff

sc qdescription Stuff

sc qc Stuff

sc start Stuff


1
给定路径 "c:\abc\def.exe",我尝试像这样传递参数 Param1="ghi": binPath= "c:\abc\def.exe /Param1=ghi"。但是没有起作用... - sympatric greg

0
我无法处理你的建议中的问题,最后在x86文件夹中只有在PowerShell(Windows Server 2012)中使用环境变量才能正常工作。
{sc.exe create svnserve binpath= "${env:programfiles(x86)}/subversion/bin/svnserve.exe --service -r C:/svnrepositories/"   displayname= "Subversion Server" depend= Tcpip start= auto}

0
W2008 Server 中,你仍然需要空格,例如 binPath= "",但在 W2019 Server 中,它已经被固定binPath="" 就可以使用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接