Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: service mode
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 25-Jan-13 15:12pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 339
2 Richard MacCutchan 225
3 BillWoodruff 219
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100