-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathService.ahk
252 lines (204 loc) · 8.71 KB
/
Service.ahk
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Windows Service Control Functions
-heresy
- Return Values
1 : Success
0 : Failure
-4 : Service Not Found
- State codes from Service_State()
SERVICE_STOPPED (1) : The service is not running.
SERVICE_START_PENDING (2) : The service is starting.
SERVICE_STOP_PENDING (3) : The service is stopping.
SERVICE_RUNNING (4) : The service is running.
SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
SERVICE_PAUSE_PENDING (6) : The service pause is pending.
SERVICE_PAUSED (7) : The service is paused.
*/
/* EXAMPLE(s)
MsgBox % Service_List("Active") ;Get List of Running Win32 Service
if Service_State("Print Spooler")=4 ;if Print Spooler service is running
Service_Stop("Print Spooler") ;stop
else if Service_State("Print Spooler")=1 ;if Print Spooler service is not running
Service_Start("Print Spooler") ;start
MsgBox % Service_State("Print Spooler")
*/
Service_List(State="", Type="", delimiter="`n") {
if !State
ServiceState := 0x3 ;SERVICE_STATE_ALL (0x00000003)
else if (State="Active")
ServiceState := 0x1 ;SERVICE_ACTIVE (0x00000001)
else if (State="Inactive")
ServiceState := 0x2 ;SERVICE_INACTIVE (0x00000002)
else
ServiceState := 0x3
if !Type
ServiceType := 0x30 ;SERVICE_WIN32 (0x00000030)
else if (Type="Driver")
ServiceType := 0xB ;SERVICE_DRIVER (0x0000000B)
else if (Type="All")
ServiceType := 0x3B ;sum of both
else
ServiceType := 0x30
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0
, "Int", 0
, "UInt", 0x4) ;SC_MANAGER_ENUMERATE_SERVICE (0x0004)
DllCall("advapi32\EnumServicesStatusA"
, "UInt", SCM_HANDLE
, "UInt", ServiceType
, "UInt", ServiceState
, "UInt", 0
, "UInt", 0
, "UIntP", bSize ;get required buffer size first
, "UIntP", 0
, "UIntP", 0)
VarSetCapacity(ENUM_SERVICE_STATUS, bSize, 0) ;prepare struct
DllCall("advapi32\EnumServicesStatusA" ;actual enumeration
, "UInt", SCM_HANDLE
, "UInt", ServiceType
, "UInt", ServiceState
, "UInt", &ENUM_SERVICE_STATUS
, "UInt", bSize
, "UIntP", 0
, "UIntP", ServiceCount
, "UIntP", 0)
Loop, %ServiceCount%
result .= DllCall("MulDiv"
, "Int", NumGet(ENUM_SERVICE_STATUS, (A_Index-1)*36+4)
, "Int", 1
, "Int", 1, "str") . delimiter
DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
Return result
}
Service_Start(ServiceName) {
ServiceName := _GetName_(ServiceName)
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0 ;NULL for local
, "Int", 0
, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
, "UInt", SCM_HANDLE
, "Str", ServiceName
, "UInt", 0x10)) ;SERVICE_START (0x0010)
result := -4 ;Service Not Found
if !result
result := DllCall("advapi32\StartServiceA"
, "UInt", SC_HANDLE
, "Int", 0
, "Int", 0)
DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
return result
}
Service_Stop(ServiceName) {
ServiceName := _GetName_(ServiceName)
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0 ;NULL for local
, "Int", 0
, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
, "UInt", SCM_HANDLE
, "Str", ServiceName
, "UInt", 0x20)) ;SERVICE_STOP (0x0020)
result := -4 ;Service Not Found
if !result
result := DllCall("advapi32\ControlService"
, "UInt", SC_HANDLE
, "Int", 1
, "Str", "")
DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
return result
}
Service_State(ServiceName) { ; Return Values
; SERVICE_STOPPED (1) : The service is not running.
; SERVICE_START_PENDING (2) : The service is starting.
; SERVICE_STOP_PENDING (3) : The service is stopping.
; SERVICE_RUNNING (4) : The service is running.
; SERVICE_CONTINUE_PENDING (5) : The service continue is pending.
; SERVICE_PAUSE_PENDING (6) : The service pause is pending.
; SERVICE_PAUSED (7) : The service is paused.
ServiceName := _GetName_(ServiceName)
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0 ;NULL for local
, "Int", 0
, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
, "UInt", SCM_HANDLE
, "Str", ServiceName
, "UInt", 0x4)) ;SERVICE_QUERY_STATUS (0x0004)
result := -4 ;Service Not Found
VarSetCapacity(SC_STATUS, 28, 0) ;SERVICE_STATUS Struct
if !result
result := !DllCall("advapi32\QueryServiceStatus"
, "UInt", SC_HANDLE
, "UInt", &SC_STATUS)
? False : NumGet(SC_STATUS, 4) ;-1 or dwCurrentState
DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
return result
}
Service_Add(ServiceName, BinaryPath, StartType=""){
if !A_IsAdmin
Return False
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0
, "Int", 0
, "UInt", 0x2) ;SC_MANAGER_CREATE_SERVICE (0x0002)
StartType := !StartType ? 0x3 : 0x2
;SERVICE_DEMAND_START(0x00000003) vs SERVICE_AUTO_START(0x00000002)
SC_HANDLE := DllCall("advapi32\CreateServiceA"
, "UInt", SCM_HANDLE
, "Str", ServiceName
, "Str", ServiceName
, "UInt", 0xF01FF ;SERVICE_ALL_ACCESS (0xF01FF)
, "UInt", 0x110 ;SERVICE_WIN32_OWN_PROCESS(0x00000010) | SERVICE_INTERACTIVE_PROCESS(0x00000100)
;interactable service with desktop (requires local account)
;http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
, "UInt", StartType
, "UInt", 0x1 ;SERVICE_ERROR_NORMAL(0x00000001)
, "Str", BinaryPath
, "Str", "" ;No Group
, "UInt", 0 ;No TagId
, "Str", "" ;No Dependencies
, "Int", 0 ;Use LocalSystem Account
, "Str", "")
result := A_LastError ? SC_HANDLE "," A_LastError : 1
DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
DllCall("advapi32\CloseServiceHandle", "UInt", SCM_HANDLE)
Return result
}
Service_Delete(ServiceName) {
if !A_IsAdmin ;Requires Administrator rights
Return False
ServiceName := _GetName_(ServiceName)
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA"
, "Int", 0 ;NULL for local
, "Int", 0
, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
if !(SC_HANDLE := DllCall("advapi32\OpenServiceA"
, "UInt", SCM_HANDLE
, "Str", ServiceName
, "UInt", 0xF01FF)) ;SERVICE_ALL_ACCESS (0xF01FF)
result := -4 ;Service Not Found
if !result
result := DllCall("advapi32\DeleteService", "Uint", SC_HANDLE)
DllCall("advapi32\CloseServiceHandle", "UInt", SC_HANDLE)
Return result
}
_GetName_(DisplayName) { ;Internal, Gets Service Name from Display Name,
SCM_HANDLE := DllCall("advapi32\OpenSCManagerA", "Int", 0, "Int", 0, "UInt", 0x1) ;SC_MANAGER_CONNECT (0x0001)
DllCall("advapi32\GetServiceKeyNameA" ;Get Buffer Size
, "Uint", SCM_HANDLE
, "Str", DisplayName
, "Int", 0
, "UintP", Len)
VarSetCapacity(Buffer, Len) ;Prepare Buffer
DllCall("advapi32\GetServiceKeyNameA" ;Get Actual Service Name
, "Uint", SCM_HANDLE
, "Str", DisplayName
, "Uint", &Buffer
, "UintP", Len)
Loop, % Len//2
Output .= Chr(NumGet(Buffer, A_Index-1, "Char"))
return !Output ? DisplayName : Output
}