Visual Basic Network Services
EnumServicesStatus: Enumerate Local Machine Services
     
Posted:   Tuesday May 22, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows NT3.1, Windows NT4, Windows 2000, Windows XP
Author:   VBnet - Randy Birch
     

Related:  

EnumServicesStatus: Enumerate Local Machine Services
EnumServicesStatus: Enumerate Local Machine Services and Status

EnumServicesStatus: Enumerate Local or Remote Machine Services and Status
     
 Prerequisites
One of the operating systems listed under OS Restrictions above.

The Win API EnumServicesStatus function enumerates services in the specified service control manager database. The name and status of each service are provided. EnumServicesStatus requires Windows NT 3.1 or later.

In order to use EnumServicesStatus, the service manager database must first be opened using a call to OpenSCManager. Once the handle to the database has been obtained, EnumServicesStatus can be called.

There are a number of flags that can be specified as the dwServiceType parameter that allows interrogation of only specific members.

This page demos only the EnumServicesStatus API using the SERVICE_STATE_ALL flag.

Note as well that EnumServicesStatus has been superseded by the EnumServicesStatusEx function for Windows 2000/XP only, which returns the same information as EnumServicesStatus plus the process identifier and additional flags for the service. In addition, EnumServicesStatusEx enables you to enumerate services that belong to a specified group.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type SERVICE_STATUS
   dwServiceType As Long
   dwCurrentState As Long
   dwControlsAccepted As Long
   dwWin32ExitCode As Long
   dwServiceSpecificExitCode As Long
   dwCheckPoint As Long
   dwWaitHint As Long
End Type

Public Type ENUM_SERVICE_STATUS
   lpServiceName As Long
   lpDisplayName As Long
   ServiceStatus As SERVICE_STATUS
End Type

'our own constant
Public Const SIZEOF_SERVICE_STATUS As Long = 36

'windows constants
Public Const ERROR_MORE_DATA = 234
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const LB_SETTABSTOPS As Long = &H192
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
                                     
'Service State
'corresponds to SERVICE_STATUS.dwCurrentState
Public Const SERVICE_STOPPED As Long = &H1
Public Const SERVICE_START_PENDING As Long = &H2
Public Const SERVICE_STOP_PENDING As Long = &H3
Public Const SERVICE_RUNNING As Long = &H4
Public Const SERVICE_CONTINUE_PENDING As Long = &H5
Public Const SERVICE_PAUSE_PENDING As Long = &H6
Public Const SERVICE_PAUSED As Long = &H7

Public Declare Function OpenSCManager Lib "advapi32" _
   Alias "OpenSCManagerA" _
  (ByVal lpMachineName As String, _
   ByVal lpDatabaseName As String, _
   ByVal dwDesiredAccess As Long) As Long

Public Declare Function EnumServicesStatus Lib "advapi32" _
   Alias "EnumServicesStatusA" _
  (ByVal hSCManager As Long, _
   ByVal dwServiceType As Long, _
   ByVal dwServiceState As Long, _
   lpServices As Any, _
   ByVal cbBufSize As Long, _
   pcbBytesNeeded As Long, _
   lpServicesReturned As Long, _
   lpResumeHandle As Long) As Long
   
Public Declare Function CloseServiceHandle Lib "advapi32" _
   (ByVal hSCObject As Long) As Long

Public Declare Function lstrcpyA Lib "kernel32" _
  (ByVal RetVal As String, ByVal Ptr As Long) As Long
                        
Public Declare Function lstrlenA Lib "kernel32" _
  (ByVal Ptr As Any) As Long

Public Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long
 Form Code
To a form add a command button (Command1) and a list box (List1). Add the following code:

Option Explicit

Private Sub Form_Load()
   
   ReDim TabArray(0 To 1) As Long
   
   TabArray(0) = 150
   TabArray(1) = 220
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 2&, TabArray(0))
   List1.Refresh
   
   Command1.Caption = "Enum Services"
   
End Sub


Private Sub Command1_Click()

   Call EnumSystemServices(List1)
      
End Sub


Public Function EnumSystemServices(ctl As Control) As Long

   Dim hSCManager As Long
   Dim pntr() As ENUM_SERVICE_STATUS
   Dim cbBuffSize As Long
   
   Dim cbRequired As Long
   Dim dwReturned As Long
   Dim hEnumResume As Long
   Dim cbBuffer As Long
   Dim success As Long
   Dim i As Long
   
  'just help to keep the code lines
  'below from becoming too long for
  'html display
   Dim sSvcName As String
   Dim sDispName As String
   Dim dwState As Long
  
  'establish a connection to the service control
  'manager on the local computer and open
  'the local service control manager database.
   hSCManager = OpenSCManager(vbNullString, _
                              vbNullString, _
                              SC_MANAGER_ENUMERATE_SERVICE)

   If hSCManager <> 0 Then

     'Get buffer size by calling EnumServicesStatus.
  
     'To determine the required buffer size, call EnumServicesStatus
     'with cbBuffer and hEnumResume set to zero. EnumServicesStatus
     'fails (returns 0), and Err.LastDLLError returns ERROR_MORE_DATA,
     'filling cbRequired with the size, in bytes, of the buffer
     'required to hold the array of structures and their data.
      success = EnumServicesStatus(hSCManager, _
                                   SERVICE_WIN32, _
                                   SERVICE_STATE_ALL, _
                                   ByVal &H0, _
                                   &H0, _
                                   cbRequired, _
                                   dwReturned, _
                                   hEnumResume)

     'If success is 0 and the LastDllError is
     'ERROR_MORE_DATA, use returned info to create
     'the required data buffer
      If success = 0 And Err.LastDllError = ERROR_MORE_DATA Then

      
        'Calculate number of structures needed
        'and redimension the array
         cbBuffer = (cbRequired \ SIZEOF_SERVICE_STATUS) + 1
         ReDim pntr(0 To cbBuffer)
   
        'Set cbBuffSize equal to the size of the buffer
         cbBuffSize = cbBuffer * SIZEOF_SERVICE_STATUS

        'Enumerate the services. If the function succeeds,
        'the return value is nonzero. If the function fails,
        'the return value is zero. In addition, hEnumResume
        'must be set to 0.
         hEnumResume = 0
         If EnumServicesStatus(hSCManager, _
                               SERVICE_WIN32, _
                               SERVICE_STATE_ALL, _
                               pntr(0), _
                               cbBuffSize, _
                               cbRequired, _
                               dwReturned, _
                               hEnumResume) Then

           'pntr() array is now filled with service data,
           'so it is a simple matter of extracting the
           'required information.
            With ctl
            
               .Clear
               
               For i = 0 To dwReturned - 1
            
                  sDispName = GetStrFromPtrA(ByVal pntr(i).lpDisplayName)
                  sSvcName = GetStrFromPtrA(ByVal pntr(i).lpServiceName)
                  dwState = pntr(i).ServiceStatus.dwCurrentState
                     
                  .AddItem sDispName & vbTab & _
                           sSvcName & vbTab & _
                           GetServiceState(dwState)
                  
               Next
            End With
            
         Else
            MsgBox "EnumServicesStatus; error " & _
                  CStr(Err.LastDllError)
         End If  'If EnumServicesStatus

   
      Else
         MsgBox "ERROR_MORE_DATA not returned; error " & _
                CStr(Err.LastDllError)
      End If  'If success = 0 And Err.LastDllError
   
   Else
      MsgBox "OpenSCManager failed; error = " & _
            CStr(Err.LastDllError)
   End If  'If hSCManager <> 0
   
  'Clean up
   Call CloseServiceHandle(hSCManager)
   
  'return the number of services
  'returned as a sign of success
   EnumSystemServices = dwReturned

End Function


Public Function GetStrFromPtrA(ByVal lpszA As Long) As String

   GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
   Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
   
End Function


Private Function GetServiceState(dwState As Long) As String
   
   Select Case dwState
      Case SERVICE_STOPPED:          GetServiceState = "stopped"
      Case SERVICE_START_PENDING:    GetServiceState = "startpend"
      Case SERVICE_STOP_PENDING:     GetServiceState = "stoppend"
      Case SERVICE_RUNNING:          GetServiceState = "running"
      Case SERVICE_CONTINUE_PENDING: GetServiceState = "contpend"
      Case SERVICE_PAUSE_PENDING:    GetServiceState = "pausepend"
      Case SERVICE_PAUSED:           GetServiceState = "paused"
   End Select

End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter