Click here to Skip to main content
6,594,432 members and growing! (15,289 online)
Email Password   helpLost your password?
Languages » C# » CodeProject Utilities     Intermediate License: The Code Project Open License (CPOL)

Service Manager

By Tina83

Search the service based on the description
C#, .NET, COM, Dev
Posted:8 Jun 2008
Views:9,044
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.68 Rating: 3.17 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
2 votes, 28.6%
3

4
3 votes, 42.9%
5

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

License

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

About the Author

Tina83


Member

Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralSuggestion PinmemberPIEBALDconsult6:18 9 Jun '08  
GeneralRe: Suggestion PinmemberMember 31284473:02 11 Jun '08  
GeneralRe: Suggestion PinmemberElfman_NE4:38 18 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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