Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

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, using OpenSCManager, then, by second step, use it as a first parameter for the call to OpenService and then, finally, use the resulting handle to the service, to call ChangeServiceConfig.

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
 
Share this answer
 
By the way, one extremely primitive alternative to Solution 1: use "SC.EXE" (Service Control Manager) via System.Diagnostics.Process.Start:

C#
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 stream StandardOutput to capture output of the application. Just in case, redirect also StandardError. You will find the code sample for redirection here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900