Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

EnumServicesStatus (advapi32)
 
.
Summary
The EnumServicesStatus function enumerates services in the specified service control manager database. The name and status of each service are provided.

C# Signature:

[DllImport("advapi32.dll", EntryPoint="EnumServicesStatusW", ExactSpelling=true, SetLastError=true)]
static extern bool EnumServicesStatus( IntPtr hSCManager,
                    SERVICE_TYPES dwServiceType,
                    SERVICE_STATES dwServiceState,
                    IntPtr lpServices,
                    int cbBufSize,
                    ref int pcbBytesNeeded,
                    ref int lpServicesReturned,
                    ref int lpResumeHandle );

VB.NET Signature:

  <DllImportAttribute("advapi32.dll", _
              EntryPoint:="EnumServicesStatusW", _
              SetLastError:=True, _
              ExactSpelling:=True, _
              CharSet:=CharSet.Unicode, _
              CallingConvention:=CallingConvention.StdCall)> _
  Shared Function EnumServicesStatus _
            (ByVal hSCManager As IntPtr, _
             ByVal dwServiceType As Integer, _
             ByVal dwServiceState As Integer, _
             ByVal lpServices As IntPtr, _
             ByVal cbBufSize As Integer, _
             ByRef pcbBytesNeeded As Integer, _
             ByRef lpServicesReturned As Integer, _
             ByRef lpResumeHandle As Integer) As Boolean
  End Function

User-Defined Types:

VB.NET:

  'Windows constants
  Public Const SERVICE_STATE_ALL = &H3

  'Service Types (Bit Mask)
  'corresponds to SERVICE_STATUS.dwServiceType
  Public Const SERVICE_KERNEL_DRIVER As Long = &H1
  Public Const SERVICE_FILE_SYSTEM_DRIVER As Long = &H2
  Public Const SERVICE_ADAPTER As Long = &H4
  Public Const SERVICE_RECOGNIZER_DRIVER As Long = &H8
  Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
  Public Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
  Public Const SERVICE_INTERACTIVE_PROCESS As Long = &H100

  Public Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS Or _
                       SERVICE_WIN32_SHARE_PROCESS

  Public Const SERVICE_DRIVER As Long = SERVICE_KERNEL_DRIVER Or
                    SERVICE_FILE_SYSTEM_DRIVER Or _
                    SERVICE_RECOGNIZER_DRIVER

  Public Const SERVICE_TYPE_ALL As Long = SERVICE_WIN32 Or _
                      SERVICE_ADAPTER Or _
                      SERVICE_DRIVER Or _
                      SERVICE_INTERACTIVE_PROCESS

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

C#

IntPtr handle = OpenSCManager( null, null, SCM_ACCESS.SC_MANAGER_ALL_ACCESS );
if ( handle != IntPtr.Zero )
{
    int iBytesNeeded = 0;
    int iServicesReturned = 0;
    int iResumeHandle = 0;

    if ( !EnumServicesStatus( handle,
        SERVICE_TYPES.SERVICE_WIN32,
        SERVICE_STATES.SERVICE_STATE_ALL,
        IntPtr.Zero,
        0,
        ref iBytesNeeded,
        ref iServicesReturned,
        ref iResumeHandle ) )
    {
        IntPtr buf = Marshal.AllocHGlobal( (int) iBytesNeeded );
        if ( !EnumServicesStatus( handle,
            SERVICE_TYPES.SERVICE_WIN32,
            SERVICE_STATES.SERVICE_STATE_ALL,
            buf,
            iBytesNeeded,
            ref iBytesNeeded,
            ref iServicesReturned,
            ref iResumeHandle ) )
            Debug.WriteLine("EnumServicesStatus Failed " + Marshal.GetLastWin32Error() );
        else
        {
            ENUM_SERVICE_STATUS serviceStatus;
            int iPtr = buf.ToInt32();
            for ( int i = 0 ; i < (int) iServicesReturned; i++ )
            {
                serviceStatus = (ENUM_SERVICE_STATUS) Marshal.PtrToStructure(
                    new IntPtr(iPtr),
                    typeof(ENUM_SERVICE_STATUS) );
                Debug.WriteLine("Service " + serviceStatus.pServiceName );
                iPtr += ENUM_SERVICE_STATUS.SizeOf;
            }
            Debug.WriteLine("EnumServicesStatus Success");
        }
    }
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions