Click here to Skip to main content
Licence CPOL
First Posted 8 Jun 2008
Views 16,045
Downloads 292
Bookmarked 23 times

Service Manager

By Tina83 | 8 Jun 2008
Search the service based on the description
1 vote, 14.3%
1
1 vote, 14.3%
2
2 votes, 28.6%
3

4
3 votes, 42.9%
5
3.17/5 - 7 votes
μ 3.17, σa 2.83 [?]

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



India India

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSuggestion PinmemberPIEBALDconsult6:18 9 Jun '08  
GeneralRe: Suggestion PinmemberMember 31284473:02 11 Jun '08  
Yes, by mistake i have pasted my trial code.
 
yes, can return servicecontroller[] but this function is according to my requirement. Big Grin | :-D
GeneralRe: Suggestion PinmemberElfman_NE4:38 18 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 9 Jun 2008
Article Copyright 2008 by Tina83
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid