Private Sub Command4_Click()
Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As Object
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("select * from win32_service")
For Each objProcess In colProcessList
debug.print cstr(objProcess.name),cstr(objProcess.state),cstr(objProcess.status)
Next
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
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
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
To a form add a command button (Command1) and a list box (List1). Add the following code: