The Code Project View our advertisersUltimate Toolbox is the must have tool for MFC Developers with hundreds of MFC extensions.Advertise on the CodeProject
Home >> .NET >> Unedited Reader Contributions

.NET Diagnostics – III, Enumerate System Services and Dependencies Using C#
By Naveen K Kohli

How to get the list of all system services on a system and also finds out their dependencies. 
 VC7, C#, .NET
 Posted 19 Mar 2001
Articles by this author
Send to a friend
Printer friendly version
[This is an unedited reader contribution] [Modify this article] [Delete this article]
What's New
Lounge
Contribute
About Us
Advertise
Forums
3 users have rated this article. result:
3.67 out of 5.

Sample Image - NetDiagnostics3.jpg

Continuing in the series of writing some diagnostics tools and exploring System.Diagnostics namespace, this article shows how do you find out what all system services are running on the system and most importantly finding their dependencies. It’s the later part that I found very interesting. Every time I open the Services dialog box or Task Manager on my system to look what all services are running, I used to think what will happen if I shut down one of them to recover some resources. But then you have to think, what if this service is part of some other service or application running on the system. This utility application enumerates and displays all the system services running on a system. And it also lists all the services a particular service is dependent upon and the services that depend on this service.

How Do You Do It

.NET SDK provides System.ServiceProcess namespace. This namespace provides classes for installing and running system services. Under this namespace, you will find a class names SystemController. A SystemController is nothing but a System Service in NT lingo. This class has a static method GetServices that returns an array of SystemController objects. This array contains all the services running on a system.

 

ServiceController  [] controllers = ServiceController.GetServices ();

 

After you have obtained this array, enumerate through each element to get details about each service. The SystemController class has a bunch of properties and their names are self-explanatory. I will not go into details of each property. I would suggest you to look in SDK documentation for more details.

 

ServiceController class supports couple of methods that can be used to start and stop a service. I have not shown the use of these methods in this utility project. But they are pretty simple to use.

Look at InitList method in the source code attached with this article. This function shows how to use all the properties of SystemController class.

 

// Check the status of this service that it can be stopped or not.

bool bCanStop = controllers[i].CanStop;

item.SetSubItem (3, bCanStop ? "Yes" : "No");

 

// Check if the service recieves notification when system shuts down.

bool bCanShutDown = controllers[i].CanShutdown;

item.SetSubItem (4, bCanShutDown ? "Yes" : "No");

 

// Check the status if the service can be paused and contiued.

bool bPauseNContinue = controllers[i].CanPauseAndContinue;

item.SetSubItem (5, bPauseNContinue ? "Yes" : "No");

 

[Top] Sign in to vote for this article:     PoorExcellent  
MSDN Magazine - Your guide to Microsoft tools, development environments, and technologies for Windows and the Web.
Premium Sponsor

View our advertisersClick here for Dundas Consulting - experts in MFC, C++, TCP/IP and ASPAdvertise on the CodeProject
Hint: For a faster board use IE 4+, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
 Search (exact phrase)
 View   Per page   Messages since
New threadMsgs 1 to 2 of 2 (Total: 2)[First] [Prev] [Next] [Last]
Subject 
Author 
Date 
  Stop time
Unconfirmed/Anonymous posting Anonymous 21:25 19 Mar '01 
  How to dynamic make a service interact with the desktop?
Unconfirmed/Anonymous posting Anonymous 21:21 19 Mar '01 
Last Visit: 12:00 Friday 1st January, 1999[First] [Prev] [Next] [Last]
Home >> .NET >> Unedited Reader Contributions
Advertise on The Code Project
Article content copyright Naveen K Kohli, 2001
everything else © CodeProject, 1999-2001.

DevelopersDexDevGuruProgrammers HeavenTek-Tips ForumsTopXMLVisualBuilder.comW3SchoolsXMLPitstopZVONSearch all Partners