65.9K
CodeProject is changing. Read more.
Home

Service Manager

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.31/5 (8 votes)

Jun 9, 2008

CPOL
viewsIcon

39291

downloadIcon

1005

Search the service based on the description

Introduction

The article will show how to get a list of system services having some description.

Background

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.

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;
     }   
}

History

  • 9th June, 2008: Initial post