Does anyone now how I can use the API to check to see the status of a service?
I need to write a program that checks the service every so often and if the status is paused or stopped I need to send an alert.
Thanks.
Printable View
Does anyone now how I can use the API to check to see the status of a service?
I need to write a program that checks the service every so often and if the status is paused or stopped I need to send an alert.
Thanks.
VB Code:
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive" ' Service Control Manager object specific access types Public Const STANDARD_RIGHTS_REQUIRED = &HF0000 Public Const SC_MANAGER_CONNECT = &H1 Public Const SC_MANAGER_CREATE_SERVICE = &H2 Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4 Public Const SC_MANAGER_LOCK = &H8 Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10 Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20 Public Const SC_MANAGER_ALL_ACCESS = _ (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT _ Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE _ Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS _ Or SC_MANAGER_MODIFY_BOOT_CONFIG) 'Service object specific access types Public Const SERVICE_QUERY_CONFIG = &H1 Public Const SERVICE_CHANGE_CONFIG = &H2 Public Const SERVICE_QUERY_STATUS = &H4 Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8 Public Const SERVICE_START = &H10 Public Const SERVICE_STOP = &H20 Public Const SERVICE_PAUSE_CONTINUE = &H40 Public Const SERVICE_INTERROGATE = &H80 Public Const SERVICE_USER_DEFINED_CONTROL = &H100 Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _ SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG _ Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS _ Or SERVICE_START Or SERVICE_STOP Or _ SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or _ SERVICE_USER_DEFINED_CONTROL) ' Service State -- for CurrentState Public Const SERVICE_STOPPED = &H1 Public Const SERVICE_START_PENDING = &H2 Public Const SERVICE_STOP_PENDING = &H3 Public Const SERVICE_RUNNING = &H4 Public Const SERVICE_CONTINUE_PENDING = &H5 Public Const SERVICE_PAUSE_PENDING = &H6 Public Const SERVICE_PAUSED = &H7 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 Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _ (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _ ByVal dwDesiredAccess As Long) As Long Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _ (ByVal hSCManager As Long, ByVal lpServiceName As String, _ ByVal dwDesiredAccess As Long) As Long Declare Function QueryServiceStatus Lib "advapi32.dll" _ (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long ' Here's where we test if the service is running Dim lhSCManager As Long Dim lhService As Long Dim lhServiceStatus As Long lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS) If lhSCManager <> 0 Then lhService = OpenService(lhSCManager, "Service Name", SERVICE_ALL_ACCESS) If lhService <> 0 Then lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus) If lhServiceStatus <> 0 Then Select Case ServiceStatus.dwCurrentState Case SERVICE_STOPPED 'do something Case SERVICE_START_PENDING 'do something Case SERVICE_STOP_PENDING 'do something Case SERVICE_RUNNING 'do something Case SERVICE_CONTINUE_PENDING 'do something Case SERVICE_PAUSE_PENDING 'do something Case SERVICE_PAUSED 'do something End Select CloseServiceHandle lhServiceStatus End If CloseServiceHandle lhService End If CloseServiceHandle lhSCManager End If
Hack,
Thanks for the code. However, I'm getting a compile error: ByRef argument type mismatch.
It highlights "ServiceStatus" in the following line of code.
Do you know what's causing this?Code:lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
Hmmm...I've used this routine before with no problems. I assume you had to tailor it to fit your indidual needs. Post exactly what you are using, and how you are using it, and I'll take a look.
At first I had two modules, one with the declarations and it called the function in the second module. I put all the code in the main module so when it executed the first thing it does is check the status.
Did I miss a setting I was suppose to change with my own data?
Here's the code
Code:Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const SC_MANAGER_ALL_ACCESS = _
(STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT _
Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE _
Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS _
Or SC_MANAGER_MODIFY_BOOT_CONFIG)
'Service object specific access types
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG _
Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS _
Or SERVICE_START Or SERVICE_STOP Or _
SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or _
SERVICE_USER_DEFINED_CONTROL)
' Service State -- for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7
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
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _
(ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
(ByVal hSCManager As Long, ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" _
(ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
' Check the state of the service
' CheckServiceState
' Here's where we test if the service is running
Sub main()
Dim lhSCManager As Long
Dim lhService As Long
Dim lhServiceStatus As Long
lhSCManager = OpenSCManager("JW2K", SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If lhSCManager <> 0 Then
lhService = OpenService(lhSCManager, "ULockService", SERVICE_ALL_ACCESS)
If lhService <> 0 Then
lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
If lhServiceStatus <> 0 Then
Select Case ServiceStatus.dwCurrentState
Case SERVICE_STOPPED
'do something
MsgBox "The lock service is stopped!", vbCritical
Case SERVICE_START_PENDING
'do something
Case SERVICE_STOP_PENDING
'do something
Case SERVICE_RUNNING
'do something
Case SERVICE_CONTINUE_PENDING
'do something
Case SERVICE_PAUSE_PENDING
'do something
Case SERVICE_PAUSED
'do something
MsgBox "The lock service is paused!", vbCritical
End Select
CloseServiceHandle lhServiceStatus
End If
CloseServiceHandle lhService
End If
CloseServiceHandle lhSCManager
End If
End Sub
Take these out of Sub Main
Dim lhSCManager As Long
Dim lhService As Long
Dim lhServiceStatus As Long
and make them Public variables instead of procedure level variables.
How are you calling Sub Main?
Sub Main is my start up procedure.
I made the variables public and received the same error.