![]() |
Languages »
C# »
CodeProject Utilities
Intermediate
License: The Code Project Open License (CPOL)
Service ManagerBy Tina83Search the service based on the description |
C#, .NET, COM, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
The article will show how to get a list of system services having some description.
I have seen many articles where we can find how to install, uninstall, start and stop a service, but in service manager I have included (with the help of my friend, Yogi) one more functionality where we can get a list of system services satisfying some description.
The .NET Framework provides the System.ServiceProcess namespace. In this namespace, you will find a class SystemController. This class has a static method GetServices() that returns an array of SystemController objects, which contains all the services running on a system.
ServiceController[] _serviceControllers;
_serviceControllers=ServiceController.GetServices();
Open service database using OpenSCManager:
IntPtr sc_handle = OpenSCManager(null,null,(int)SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
Open and query the service for the description:
public static string[] GetServices(string filterOnDescription)
{
ServiceController[] _serviceControllers;
_serviceControllers=ServiceController.GetServices();
string[] _serviceList = null;
System.Collections.ArrayList _arrayList = new System.Collections.ArrayList();
IntPtr sc_handle = OpenSCManager(null,null,(int)SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
if (sc_handle.ToInt32() != 0)
{
IntPtr serviceHandle;
SERVICE_DESCRIPTION descriptionStruct = new SERVICE_DESCRIPTION();
for (int i=0;i<_serviceControllers.Length;i++)
{
UInt32 dwBytesNeeded;
serviceHandle = OpenService
( sc_handle, _serviceControllers[i].ServiceName,
SERVICE_QUERY_CONFIG );
if ( serviceHandle != IntPtr.Zero )
{
// Determine the buffer size needed
bool success = QueryServiceConfig2( serviceHandle,
SERVICE_CONFIG_DESCRIPTION, IntPtr.Zero, 0, out dwBytesNeeded );
IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);
success = QueryServiceConfig2( serviceHandle,
SERVICE_CONFIG_DESCRIPTION, ptr,
dwBytesNeeded, out dwBytesNeeded );
Marshal.PtrToStructure(ptr, descriptionStruct);
Marshal.FreeHGlobal(ptr);
if((descriptionStruct.lpDescription != null) && (
descriptionStruct.lpDescription.IndexOf(filterOnDescription)!=-1))
_arrayList.Add(_serviceControllers[i].ServiceName);
}
}
if(_arrayList.Count > 0)
_serviceList = (string[])_arrayList.ToArray(typeof(string));
return _serviceList;
}
else
{
return null;
}
}
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Jun 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Tina83 Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |