如何通过C#显示特定服务的服务启动模式 [英] How to display Service Start Mode for a specific service thru C#
问题描述
您好
我正试图找到一种方法,只需显示特定
Windows服务的启动模式。例如...如果我只想点击一个按钮并让它显示W3SVC的服务状态和启动类型?
我尝试使用Servicecontroller但是这并没有给我这个选项。有没有一种简单的方法呢?
基本上,您需要打开服务管理,与管理员一起打开服务,然后更改配置。三个步骤:
http ://msdn.microsoft.com/en-us/library/windows/desktop/ms684323%28v=vs.85%29.aspx [ ^ ],
< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms684330%28v=vs.85%29.aspx> http://msdn.microsoft.com/en-us /library/windows/desktop/ms684330%28v=vs.85%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681987%28v=vs .85%29.aspx [ ^ ]。
第一步,使用OpenSCManager获取服务管理器的句柄
SC_HANDLE
code>,然后,第二步,使用它作为调用OpenService
的第一个参数,然后,最后,使用生成的服务句柄,到调用ChangeServiceConfig
。
你需要P / Invoke。如果您需要学习P / Invoke,请从这里开始:
http://en.wikipedia .org / wiki / P / Invoke [ ^ ],
http://msdn.microsoft.com/en-us/library / Aa712982 [ ^ ]。
此CodeProject也很有用: Essential P / Invoke [ ^ ]。
P / Invoke的一种替代方法是使用C ++ / CLI混合模式(托管+非托管)项目。此项目可以构建全局钩子所需的DLL,但它可以包含一些托管包装器CLI代码,因此,从.NET应用程序的角度来看,您可以将其用作常规.NET程序集,即引用它。请参阅:
http://en.wikipedia.org/wiki/C %2B%2B / CLI [ ^ ],
http://www.ecma-international .org / publications / standards / Ecma-372.htm [ ^ ],
http:/ /msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ],
http:/ /msdn.microsoft.com/en-us/library/3bstk3k5 [ ^ ]。
这样,y你可以用C ++编写代码,将它包装在C ++ / CLI托管类中,并将生成的模块作为.NET程序集引用。
-SA
顺便提一下,解决方案1的一个非常原始的替代方案:通过System.Diagnostics.Process.Start使用SC.EXE(服务控制管理器)
:
System.ServiceProcess.ServiceStartMode startMode = / / ...
string processName = // ...
// ...
string application = SC;
string commandLine = string .Format(
CONFIG {0},start = {1}, // important:'='之后的空格
processName,startMode);
System.Diagnostics.Process.Start(application,commandLine);
您还可以重定向流StandardOutput
以捕获应用程序的输出。为了以防万一,还重定向StandardError
。你会在这里找到重定向的代码示例:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [ ^ ]。
- SA
Hello
I am trying to find a way to just simply display the Start Mode for a specific
Windows Services. For example...If I just want to click on a button and have it display the Service Status and Start Type for W3SVC ?
I tried using Servicecontroller but that does not give me this option. Is there a simple way of doing this?
Basically, you need to open service manages, with the manager, open a service, and then change it configuration. Three steps:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684330%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681987%28v=vs.85%29.aspx[^].
By first step, you get the handle of the Service Manager,SC_HANDLE
, usingOpenSCManager
, then, by second step, use it as a first parameter for the call toOpenService
and then, finally, use the resulting handle to the service, to callChangeServiceConfig
.
You will need P/Invoke. If you need to learn P/Invoke, start from here:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].
This CodeProject can also be useful: Essential P/Invoke[^].
One alternative to P/Invoke is to use C++/CLI mixed-mode (managed+unmanaged) project. This project could build a DLL required by the global hooks, but it can contain some managed wrapper CLI code, so, from the standpoint of your .NET application, you can use it as a regular .NET assembly, that is, reference it. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^],
http://msdn.microsoft.com/en-us/library/3bstk3k5[^].
This way, you can write the code in C++, wrap it in C++/CLI managed class and reference the resulting module as a .NET assembly.
—SA
By the way, one extremely primitive alternative to Solution 1: use "SC.EXE" (Service Control Manager) viaSystem.Diagnostics.Process.Start
:
System.ServiceProcess.ServiceStartMode startMode = // ... string processName = // ... //... string application = "SC"; string commandLine = string.Format( "CONFIG {0}, start= {1}", // important: blank space after '=' processName, startMode); System.Diagnostics.Process.Start(application, commandLine);
You can also redirect the streamStandardOutput
to capture output of the application. Just in case, redirect alsoStandardError
. You will find the code sample for redirection here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].
—SA
这篇关于如何通过C#显示特定服务的服务启动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!