VB and VBA Users Source Code: Start, Stop and Pause NT Services
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Start, Stop and Pause NT Services
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, January 01, 2001
Hits:
1453
Category:
Networks
Article:
The following routines can be used to control NT Services on a local or remote machine. A demonstration routine is at the bottom of the post: Option Explicit Public Enum eServiceState essStopService essStartService essPauseService End Enum Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long Private Declare Function ControlService Lib "advapi32.dll" (ByVal lHwndService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long Private Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal lHwndService As Long, lpServiceStatus As SERVICE_STATUS) As Long Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal lHwndService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long Private Const SERVICES_ACTIVE_DATABASE = "ServicesActive" Private Const SERVICE_CONTROL_STOP = &H1 Private Const SERVICE_CONTROL_PAUSE = &H2 Private Const SERVICE_STOPPED = &H1 Private Const SERVICE_START_PENDING = &H2 Private Const SERVICE_STOP_PENDING = &H3 Private Const SERVICE_RUNNING = &H4 Private Const SERVICE_CONTINUE_PENDING = &H5 Private Const SERVICE_PAUSE_PENDING = &H6 Private Const SERVICE_PAUSED = &H7 Private Const STANDARD_RIGHTS_REQUIRED = &HF0000 Private Const SC_MANAGER_CONNECT = &H1 Private Const SC_MANAGER_CREATE_SERVICE = &H2 Private Const SC_MANAGER_ENUMERATE_SERVICE = &H4 Private Const SC_MANAGER_LOCK = &H8 Private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10 Private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20 Private 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) Private Const SERVICE_QUERY_CONFIG = &H1 Private Const SERVICE_CHANGE_CONFIG = &H2 Private Const SERVICE_QUERY_STATUS = &H4 Private Const SERVICE_ENUMERATE_DEPENDENTS = &H8 Private Const SERVICE_START = &H10 Private Const SERVICE_STOP = &H20 Private Const SERVICE_PAUSE_CONTINUE = &H40 Private Const SERVICE_INTERROGATE = &H80 Private Const SERVICE_USER_DEFINED_CONTROL = &H100 Private 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) Private 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 'Purpose : Returns the status of a NT Service 'Inputs : sServiceName The name of the service to test ' [sComputerName] The name of the machine to test the service status on. ' If unspecified uses the local machine 'Outputs : Returns a English description of the service status 'Author : Andrew Baker 'Date : 01/01/2001 21:07 'Notes : 'Revisions : Public Function ServiceStatus(sServiceName As String, Optional sComputerName As String) As String Dim tServiceStat As SERVICE_STATUS Dim lHwndSManager As Long Dim lHwndService As Long Dim hServiceStatus As Long 'Check the input data If InStr(1, sServiceName, " ") Then Debug.Print "Service names cannot contain spaces. Use the 'Service Name' of the service, not the 'Display Name'" Exit Function End If ServiceStatus = "" 'Open the service manager lHwndSManager = OpenSCManager(sComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS) If lHwndSManager <> 0 Then 'Open the service lHwndService = OpenService(lHwndSManager, sServiceName, SERVICE_ALL_ACCESS) If lHwndService <> 0 Then 'Query the service hServiceStatus = QueryServiceStatus(lHwndService, tServiceStat) If hServiceStatus <> 0 Then Select Case tServiceStat.dwCurrentState Case SERVICE_STOPPED ServiceStatus = "Stopped" Case SERVICE_START_PENDING ServiceStatus = "Start Pending" Case SERVICE_STOP_PENDING ServiceStatus = "Stop Pending" Case SERVICE_RUNNING ServiceStatus = "Running" Case SERVICE_CONTINUE_PENDING ServiceStatus = "Coninue Pending" Case SERVICE_PAUSE_PENDING ServiceStatus = "Pause Pending" Case SERVICE_PAUSED ServiceStatus = "Paused" End Select End If 'Close the service CloseServiceHandle lHwndService End If 'Close the service mananger CloseServiceHandle lHwndSManager End If End Function 'Purpose : Changes the state of an NT Service 'Inputs : sServiceName The name of the service to test ' [sComputerName] The name of the machine to test the service status on. ' If unspecified uses the local machine 'Outputs : N/A 'Author : Andrew Baker 'Date : 01/01/2001 21:07 'Notes : 'Revisions : Public Function ServiceStateChange(sServiceName As String, eState As eServiceState, Optional sComputerName As String) As Boolean Dim tServiceStatus As SERVICE_STATUS Dim lHwndSManager As Long Dim lHwndService As Long Dim lRes As Long 'Check the input data If InStr(1, sServiceName, " ") Then Debug.Print "Service names cannot contain spaces. Use the 'Service Name' of the service, not the 'Display Name'" ServiceStateChange = False Exit Function End If 'Open the service manager lHwndSManager = OpenSCManager(sComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS) If lHwndSManager <> 0 Then 'Open the service lHwndService = OpenService(lHwndSManager, sServiceName, SERVICE_ALL_ACCESS) If lHwndService <> 0 Then Select Case eState Case essPauseService 'Pause the service lRes = ControlService(lHwndService, SERVICE_CONTROL_PAUSE, tServiceStatus) Case essStartService 'Start the service lRes = StartService(lHwndService, 0, 0) Case essStopService lRes = ControlService(lHwndService, SERVICE_CONTROL_STOP, tServiceStatus) Case Else Debug.Print "Invalid Service State" Debug.Assert False End Select If lRes Then 'Success ServiceStateChange = True Else 'Failed ServiceStateChange = False Debug.Print "Error in ServiceStateChange: " & Err.LastDllError Debug.Assert False End If CloseServiceHandle lHwndService End If CloseServiceHandle lHwndSManager Else Debug.Print "Failed to open service mananger!" Debug.Assert False ServiceStateChange = False End If End Function 'Demonstration routine using the "Windows Time" service Sub Test() Dim sStatus As String sStatus = ServiceStatus("W32Time") Debug.Print "Event Log is now " & sStatus Call ServiceStateChange("W32Time", essStopService) sStatus = ServiceStatus("W32Time") Debug.Print "Event Log is now " & sStatus Call ServiceStateChange("W32Time", essStartService) sStatus = ServiceStatus("Eventlog") Debug.Print "Event Log is now " & sStatus End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder