1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2010, Vivek Nigam All Rights Reserved.
' Code contain copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' #VBCode#************************************************************
' * Programmer Name : Vivek Nigam
' * Web Site :
' * E-Mail : vivek_nigam19@hotmail.com.com
' * Date : 11/08/2004
' * Time : 10:10 AM
' * Module Name : Start, Stop, Pause and get status of NT Services
' * Module Filename : pServiceStatus.vbp
' * Procedure Name :
' * Parameters :
' *
' **********************************************************************
' * Comments : Start, Stop, Pause and get status of NT Services
' * : Start a NT Services | Stop a NT Services | Pause a NT Services | Status a NT Services
' * : Use advapi32.dll to Start, Stop, Pause and get status of NT Services
' **********************************************************************
'API Constants
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2
Public Const SERVICE_CONTROL_CONTINUE = &H3
' 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
'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)
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 CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
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
Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
'getsion erreur
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const LANG_NEUTRAL = &H0
Private Const SUBLANG_DEFAULT = &H1
Private Const ERROR_BAD_USERNAME = 2202&
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Public Function ServiceStatus(ComputerName As String, ServiceName As String) As String
Dim ServiceStat As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim hServiceStatus As Long
ServiceStatus = "Pending"
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
hServiceStatus = QueryServiceStatus(hService, ServiceStat)
If hServiceStatus <> 0 Then
Select Case ServiceStat.dwCurrentState
Case SERVICE_STOPPED
ServiceStatus = "Stopped"
Case SERVICE_RUNNING
ServiceStatus = "Running"
Case SERVICE_PAUSED
ServiceStatus = "Paused"
Case SERVICE_START_PENDING
ServiceStatus = "Start Pending"
Case SERVICE_STOP_PENDING
ServiceStatus = "Stop Pending"
Case SERVICE_CONTINUE_PENDING
ServiceStatus = "Continue Pending"
Case SERVICE_PAUSE_PENDING
ServiceStatus = "Pause Pending"
End Select
Else
AffErr
End If
CloseServiceHandle hService
Else
ServiceStatus = "UnInstalled"
End If
CloseServiceHandle hSManager
End If
End Function
Public Sub ServicePause(ComputerName As String, ServiceName As String)
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim lRet As Long
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
lRet = ControlService(hService, SERVICE_CONTROL_PAUSE, ServiceStatus)
If lRet = 0 Then
AffErr
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Sub
Public Sub ServiceContinue(ComputerName As String, ServiceName As String)
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim lRet As Long
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
lRet = ControlService(hService, SERVICE_CONTROL_CONTINUE, ServiceStatus)
If lRet = 0 Then
AffErr
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Sub
Public Sub ServiceStart(ComputerName As String, ServiceName As String)
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim lRet As Long
DoEvents
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
lRet = StartService(hService, 0, 0)
If lRet = 0 Then
AffErr
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Sub
Public Sub ServiceStop(ComputerName As String, ServiceName As String)
Dim ServiceStatus As SERVICE_STATUS
Dim hSManager As Long
Dim hService As Long
Dim lRet As Long
hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
If hSManager <> 0 Then
hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
lRet = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
If lRet = 0 Then
AffErr
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSManager
End If
End Sub
Private Sub AffErr()
Dim Buffer As String
Buffer = Space(200)
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, Err.LastDllError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
MsgBox Buffer
End Sub |
Partager