Thread: enumerate the list of services associated with a Process in C#

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    enumerate the list of services associated with a Process in C#

    hi everyone

    i'm looking for a way to display the service(s) associated with a process (eg, services associated with svchost.exe). Searching the net i found that "QueryServiceStatusEx" is the way to go, i looked in MSDN but because i have very little Win32 API experience i got lost in their example and im struggling implementing it in my C# project.

    can anyone please give me a head start?a code example would be great.

    Thank you

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    a code example would be great.
    Some starter code using Pinvoke. You'll have to do the QueryServiceStatus Ex function.
    Code:
    using System;
    using System.Runtime.InteropServices;
    
    class MyQueryServiceStatus
    {
        [DllImport("advapi32.dll", EntryPoint="OpenSCManagerW", ExactSpelling=true, CharSet=CharSet.Unicode, SetLastError=true)]
            public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
    
        [DllImport("advapi32.dll", SetLastError=true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool CloseServiceHandle( IntPtr hSCObject );
    
        [Flags]
            enum ACCESS_MASK : uint
            {
            	DELETE = 0x00010000,
                READ_CONTROL = 0x00020000,
                WRITE_DAC = 0x00040000,
                WRITE_OWNER = 0x00080000,
                SYNCHRONIZE = 0x00100000,
    
                STANDARD_RIGHTS_REQUIRED = 0x000f0000,
    
                STANDARD_RIGHTS_READ = 0x00020000,
                STANDARD_RIGHTS_WRITE = 0x00020000,
                STANDARD_RIGHTS_EXECUTE = 0x00020000,
    
                STANDARD_RIGHTS_ALL = 0x001f0000,
    
                SPECIFIC_RIGHTS_ALL = 0x0000ffff,
    
                ACCESS_SYSTEM_SECURITY = 0x01000000,
    
                MAXIMUM_ALLOWED = 0x02000000,
    
                GENERIC_READ = 0x80000000,
                GENERIC_WRITE = 0x40000000,
                GENERIC_EXECUTE = 0x20000000,
                GENERIC_ALL = 0x10000000,
    
                DESKTOP_READOBJECTS = 0x00000001,
                DESKTOP_CREATEWINDOW = 0x00000002,
                DESKTOP_CREATEMENU = 0x00000004,
                DESKTOP_HOOKCONTROL = 0x00000008,
                DESKTOP_JOURNALRECORD = 0x00000010,
                DESKTOP_JOURNALPLAYBACK = 0x00000020,
                DESKTOP_ENUMERATE = 0x00000040,
                DESKTOP_WRITEOBJECTS = 0x00000080,
                DESKTOP_SWITCHDESKTOP = 0x00000100,
    
                WINSTA_ENUMDESKTOPS = 0x00000001,
                WINSTA_READATTRIBUTES = 0x00000002,
                WINSTA_ACCESSCLIPBOARD = 0x00000004,
                WINSTA_CREATEDESKTOP = 0x00000008,
                WINSTA_WRITEATTRIBUTES = 0x00000010,
                WINSTA_ACCESSGLOBALATOMS = 0x00000020,
                WINSTA_EXITWINDOWS = 0x00000040,
                WINSTA_ENUMERATE = 0x00000100,
                WINSTA_READSCREEN = 0x00000200,
    
                WINSTA_ALL_ACCESS = 0x0000037f
        }
    
        [Flags]
            public enum SERVICE_STATE : int
            {
            	SERVICE_ACTIVE      = 0x00000001,
                SERVICE_INACTIVE    = 0x00000002,
                SERVICE_STATE_ALL   = SERVICE_ACTIVE | SERVICE_INACTIVE
        }
    
        [Flags]
            public enum SERVICE_TYPES : int
            {
            	SERVICE_KERNEL_DRIVER = 0x00000001,
                SERVICE_FILE_SYSTEM_DRIVER = 0x00000002,
                SERVICE_ADAPTER = 0x00000004,    
                SERVICE_RECOGNIZER_DRIVER = 0x00000008,    
                SERVICE_DRIVER = SERVICE_KERNEL_DRIVER |
                SERVICE_FILE_SYSTEM_DRIVER |
                SERVICE_RECOGNIZER_DRIVER,
    
                SERVICE_WIN32_OWN_PROCESS = 0x00000010,
                SERVICE_WIN32_SHARE_PROCESS = 0x00000020,
                SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS |
                SERVICE_WIN32_SHARE_PROCESS,
        }
    
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct SERVICE_STATUS 
            {
            public static readonly int SizeOf = Marshal.SizeOf(typeof(SERVICE_STATUS));
            public SERVICE_TYPES dwServiceType;
            public SERVICE_STATE dwCurrentState;  
            public uint dwControlsAccepted;  
            public uint dwWin32ExitCode;  
            public uint dwServiceSpecificExitCode;  
            public uint dwCheckPoint;  
            public uint dwWaitHint;
        } 
    
        [Flags]
            public enum SCM_ACCESS : uint
              {
            /// <summary>
            /// Required to connect to the service control manager.
            /// </summary>
            SC_MANAGER_CONNECT = 0x00001,
    
                /// <summary>
                /// Required to call the CreateService function to create a service
                /// object and add it to the database.
                /// </summary>
                SC_MANAGER_CREATE_SERVICE = 0x00002,
    
                /// <summary>
                /// Required to call the EnumServicesStatusEx function to list the 
                /// services that are in the database.
                /// </summary>
                SC_MANAGER_ENUMERATE_SERVICE = 0x00004,
    
                /// <summary>
                /// Required to call the LockServiceDatabase function to acquire a 
                /// lock on the database.
                /// </summary>
                SC_MANAGER_LOCK = 0x00008,
    
                /// <summary>
                /// Required to call the QueryServiceLockStatus function to retrieve 
                /// the lock status information for the database.
                /// </summary>
                SC_MANAGER_QUERY_LOCK_STATUS = 0x00010,
    
                /// <summary>
                /// Required to call the NotifyBootConfigStatus function.
                /// </summary>
                SC_MANAGER_MODIFY_BOOT_CONFIG = 0x00020,
    
                /// <summary>
                /// Includes STANDARD_RIGHTS_REQUIRED, in addition to all access 
                /// rights in this table.
                /// </summary>
                SC_MANAGER_ALL_ACCESS = ACCESS_MASK.STANDARD_RIGHTS_REQUIRED |
                SC_MANAGER_CONNECT |
                SC_MANAGER_CREATE_SERVICE |
                SC_MANAGER_ENUMERATE_SERVICE |
                SC_MANAGER_LOCK |
                SC_MANAGER_QUERY_LOCK_STATUS |
                SC_MANAGER_MODIFY_BOOT_CONFIG,
    
                GENERIC_READ = ACCESS_MASK.STANDARD_RIGHTS_READ |
                SC_MANAGER_ENUMERATE_SERVICE |
                SC_MANAGER_QUERY_LOCK_STATUS,
    
                GENERIC_WRITE = ACCESS_MASK.STANDARD_RIGHTS_WRITE |
                SC_MANAGER_CREATE_SERVICE |
                SC_MANAGER_MODIFY_BOOT_CONFIG,
    
                GENERIC_EXECUTE = ACCESS_MASK.STANDARD_RIGHTS_EXECUTE |
                SC_MANAGER_CONNECT | SC_MANAGER_LOCK,
    
                GENERIC_ALL = SC_MANAGER_ALL_ACCESS,
        }
    
        public static void Main() 
        {
            IntPtr handle = OpenSCManager( null, null,  (uint)SCM_ACCESS.SC_MANAGER_ALL_ACCESS );
            if ( handle != IntPtr.Zero )
            {
                Console.WriteLine("OpenSCManager Successful");
                //QueryServiceStatusEx goes here
                CloseServiceHandle( handle );
            }
            else
            {
                Console.WriteLine("OpenSCManager Failed");  
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM