我查了一下,找不到一个简单的问题:
Windows 服务如何确定其启动的 ServiceName?
我知道安装可以破解注册表并添加命令行参数,但从逻辑上讲,这似乎是这样should没有必要,所以才有这个问题。
我希望比注册表黑客更干净地运行单个二进制文件的多个副本。
Edit:
这是用 C# 编写的。我的应用程序Main()入口点做不同的事情,具体取决于
命令行参数:
- 安装或卸载服务。命令行可以提供非默认的
ServiceName并可以改变工作线程的数量。
- 作为命令行可执行文件运行(用于调试),
- 作为“Windows 服务”运行。在这里,它创建了我的一个实例服务库-衍生的
类,然后调用System.ServiceProcess.ServiceBase.Run(实例);
目前,安装步骤将服务名称和线程数附加到图像路径在注册表中,以便应用程序可以确定它的 ServiceName。
From: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=387024 https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=387024
这是一个 WMI 解决方案。覆盖ServiceBase.ServiceMainCallback()可能也有用,但这似乎对我有用......
protected String GetServiceName()
{
// Calling System.ServiceProcess.ServiceBase::ServiceNamea allways returns
// an empty string,
// see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=387024
// So we have to do some more work to find out our service name, this only works if
// the process contains a single service, if there are more than one services hosted
// in the process you will have to do something else
int processId = System.Diagnostics.Process.GetCurrentProcess().Id;
String query = "SELECT * FROM Win32_Service where ProcessId = " + processId;
System.Management.ManagementObjectSearcher searcher =
new System.Management.ManagementObjectSearcher(query);
foreach (System.Management.ManagementObject queryObj in searcher.Get()) {
return queryObj["Name"].ToString();
}
throw new Exception("Can not get the ServiceName");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)