001.
Start,
Stop
and Pause NT Services
002.
003.
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:
004.
005.
Option
Explicit
006.
007.
Public
Enum
eServiceState
008.
essStopService
009.
essStartService
010.
essPauseService
011.
End
Enum
012.
013.
Private
Declare
Function
CloseServiceHandle
Lib
"advapi32.dll"
(
ByVal
hSCObject
As
Long
)
As
Long
014.
Private
Declare
Function
ControlService
Lib
"advapi32.dll"
(
ByVal
lHwndService
As
Long
,
ByVal
dwControl
As
Long
, lpServiceStatus
As
SERVICE_STATUS)
As
Long
015.
Private
Declare
Function
OpenSCManager
Lib
"advapi32.dll"
Alias
"OpenSCManagerA"
(
ByVal
lpMachineName
As
String
,
ByVal
lpDatabaseName
As
String
,
ByVal
dwDesiredAccess
As
Long
)
As
Long
016.
Private
Declare
Function
OpenService
Lib
"advapi32.dll"
Alias
"OpenServiceA"
(
ByVal
hSCManager
As
Long
,
ByVal
lpServiceName
As
String
,
ByVal
dwDesiredAccess
As
Long
)
As
Long
017.
Private
Declare
Function
QueryServiceStatus
Lib
"advapi32.dll"
(
ByVal
lHwndService
As
Long
, lpServiceStatus
As
SERVICE_STATUS)
As
Long
018.
Private
Declare
Function
StartService
Lib
"advapi32.dll"
Alias
"StartServiceA"
(
ByVal
lHwndService
As
Long
,
ByVal
dwNumServiceArgs
As
Long
,
ByVal
lpServiceArgVectors
As
Long
)
As
Long
019.
020.
Private
Const
SERVICES_ACTIVE_DATABASE =
"ServicesActive"
021.
Private
Const
SERVICE_CONTROL_STOP = &H1
022.
Private
Const
SERVICE_CONTROL_PAUSE = &H2
023.
Private
Const
SERVICE_STOPPED = &H1
024.
Private
Const
SERVICE_START_PENDING = &H2
025.
Private
Const
SERVICE_STOP_PENDING = &H3
026.
Private
Const
SERVICE_RUNNING = &H4
027.
Private
Const
SERVICE_CONTINUE_PENDING = &H5
028.
Private
Const
SERVICE_PAUSE_PENDING = &H6
029.
Private
Const
SERVICE_PAUSED = &H7
030.
Private
Const
STANDARD_RIGHTS_REQUIRED = &HF0000
031.
Private
Const
SC_MANAGER_CONNECT = &H1
032.
Private
Const
SC_MANAGER_CREATE_SERVICE = &H2
033.
Private
Const
SC_MANAGER_ENUMERATE_SERVICE = &H4
034.
Private
Const
SC_MANAGER_LOCK = &H8
035.
Private
Const
SC_MANAGER_QUERY_LOCK_STATUS = &H10
036.
Private
Const
SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
037.
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)
038.
Private
Const
SERVICE_QUERY_CONFIG = &H1
039.
Private
Const
SERVICE_CHANGE_CONFIG = &H2
040.
Private
Const
SERVICE_QUERY_STATUS = &H4
041.
Private
Const
SERVICE_ENUMERATE_DEPENDENTS = &H8
042.
Private
Const
SERVICE_START = &H10
043.
Private
Const
SERVICE_STOP = &H20
044.
Private
Const
SERVICE_PAUSE_CONTINUE = &H40
045.
Private
Const
SERVICE_INTERROGATE = &H80
046.
Private
Const
SERVICE_USER_DEFINED_CONTROL = &H100
047.
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)
048.
049.
Private
Type SERVICE_STATUS
050.
dwServiceType
As
Long
051.
dwCurrentState
As
Long
052.
dwControlsAccepted
As
Long
053.
dwWin32ExitCode
As
Long
054.
dwServiceSpecificExitCode
As
Long
055.
dwCheckPoint
As
Long
056.
dwWaitHint
As
Long
057.
End
Type
058.
059.
060.
061.
062.
063.
064.
065.
066.
067.
Public
Function
ServiceStatus(sServiceName
As
String
,
Optional
sComputerName
As
String
)
As
String
068.
Dim
tServiceStat
As
SERVICE_STATUS
069.
Dim
lHwndSManager
As
Long
070.
Dim
lHwndService
As
Long
071.
Dim
hServiceStatus
As
Long
072.
073.
074.
If
InStr(1, sServiceName,
" "
)
Then
075.
Debug.Print
"Service names cannot contain spaces. Use the 'Service Name' of the service, not the 'Display Name'"
076.
Exit
Function
077.
End
If
078.
079.
ServiceStatus =
""
080.
081.
lHwndSManager = OpenSCManager(sComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
082.
If
lHwndSManager <> 0
Then
083.
084.
lHwndService = OpenService(lHwndSManager, sServiceName, SERVICE_ALL_ACCESS)
085.
If
lHwndService <> 0
Then
086.
087.
hServiceStatus = QueryServiceStatus(lHwndService, tServiceStat)
088.
If
hServiceStatus <> 0
Then
089.
Select
Case
tServiceStat.dwCurrentState
090.
Case
SERVICE_STOPPED
091.
ServiceStatus =
"Stopped"
092.
Case
SERVICE_START_PENDING
093.
ServiceStatus =
"Start Pending"
094.
Case
SERVICE_STOP_PENDING
095.
ServiceStatus =
"Stop Pending"
096.
Case
SERVICE_RUNNING
097.
ServiceStatus =
"Running"
098.
Case
SERVICE_CONTINUE_PENDING
099.
ServiceStatus =
"Coninue Pending"
100.
Case
SERVICE_PAUSE_PENDING
101.
ServiceStatus =
"Pause Pending"
102.
Case
SERVICE_PAUSED
103.
ServiceStatus =
"Paused"
104.
End
Select
105.
End
If
106.
107.
CloseServiceHandle lHwndService
108.
End
If
109.
110.
CloseServiceHandle lHwndSManager
111.
End
If
112.
End
Function
113.
114.
115.
116.
117.
118.
119.
120.
121.
Public
Function
ServiceStateChange(sServiceName
As
String
, eState
As
eServiceState,
Optional
sComputerName
As
String
)
As
Boolean
122.
Dim
tServiceStatus
As
SERVICE_STATUS
123.
Dim
lHwndSManager
As
Long
124.
Dim
lHwndService
As
Long
125.
Dim
lRes
As
Long
126.
127.
128.
If
InStr(1, sServiceName,
" "
)
Then
129.
Debug.Print
"Service names cannot contain spaces. Use the 'Service Name' of the service, not the 'Display Name'"
130.
ServiceStateChange =
False
131.
Exit
Function
132.
End
If
133.
134.
135.
lHwndSManager = OpenSCManager(sComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
136.
If
lHwndSManager <> 0
Then
137.
138.
lHwndService = OpenService(lHwndSManager, sServiceName, SERVICE_ALL_ACCESS)
139.
If
lHwndService <> 0
Then
140.
Select
Case
eState
141.
Case
essPauseService
142.
143.
lRes = ControlService(lHwndService, SERVICE_CONTROL_PAUSE, tServiceStatus)
144.
Case
essStartService
145.
146.
lRes = StartService(lHwndService, 0, 0)
147.
Case
essStopService
148.
lRes = ControlService(lHwndService, SERVICE_CONTROL_STOP, tServiceStatus)
149.
Case
Else
150.
Debug.Print
"Invalid Service State"
151.
Debug.Assert
False
152.
End
Select
153.
154.
If
lRes
Then
155.
156.
ServiceStateChange =
True
157.
Else
158.
159.
ServiceStateChange =
False
160.
Debug.Print
"Error in ServiceStateChange: "
& Err.LastDllError
161.
Debug.Assert
False
162.
End
If
163.
CloseServiceHandle lHwndService
164.
End
If
165.
CloseServiceHandle lHwndSManager
166.
Else
167.
Debug.Print
"Failed to open service mananger!"
168.
Debug.Assert
False
169.
ServiceStateChange =
False
170.
End
If
171.
End
Function
172.
173.
174.
Sub
Test()
175.
Dim
sStatus
As
String
176.
177.
sStatus = ServiceStatus(
"W32Time"
)
178.
Debug.Print
"Event Log is now "
& sStatus
179.
180.
Call
ServiceStateChange(
"W32Time"
, essStopService)
181.
sStatus = ServiceStatus(
"W32Time"
)
182.
Debug.Print
"Event Log is now "
& sStatus
183.
184.
Call
ServiceStateChange(
"W32Time"
, essStartService)
185.
sStatus = ServiceStatus(
"Eventlog"
)
186.
Debug.Print
"Event Log is now "
& sStatus
187.
End
Sub