-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceController.cpp
384 lines (338 loc) · 10.2 KB
/
ServiceController.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ServiceController.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TServiceController *)
{
new TServiceController(NULL);
}
//---------------------------------------------------------------------------
__fastcall TServiceController::TServiceController(TComponent *Owner)
: TComponent(Owner)
{
Status = (LPSERVICE_STATUS)malloc(sizeof(SERVICE_STATUS));
FServiceName = new AnsiString();
FStatusString = new AnsiString();
FStatus = None;
ManagerOpen = false;
ManHandle = NULL;
SvcHandle = NULL;
}
//---------------------------------------------------------------------------
__fastcall TServiceController::~TServiceController()
{
if (SvcHandle != NULL)
CloseServiceHandle(SvcHandle);
if (ManagerOpen)
CloseManager();
delete FStatusString;
delete FServiceName;
if (Status != NULL)
free(Status);
}
//---------------------------------------------------------------------------
namespace Servicecontroller
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TServiceController)};
RegisterComponents("Celestial", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::OpenManager(bool InstallAccess) {
if (!InstallAccess)
ManHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT & GENERIC_READ & GENERIC_WRITE & GENERIC_EXECUTE);
else
ManHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (ManHandle != NULL) {
ManagerOpen = true;
CanInstall = InstallAccess;
}
else {
DWORD res = GetLastError();
ThrowException(res);
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::CloseManager() {
if (CloseServiceHandle(ManHandle) == ERROR_INVALID_HANDLE)
throw Exception("Invalid Service Control Manager handle");
ManHandle = NULL;
ManagerOpen = false;
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::OpenServ(AnsiString ServName) {
if (!ManagerOpen)
OpenManager(false);
if (SvcHandle != NULL)
CloseServ();
SvcHandle = OpenService(ManHandle, ServName.c_str(),
SERVICE_ALL_ACCESS);
if (SvcHandle == NULL) {
DWORD res = GetLastError();
ThrowException(res);
}
else
*FServiceName = ServName;
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::CloseServ() {
if (CloseServiceHandle(SvcHandle) == ERROR_INVALID_HANDLE)
throw Exception("Invalid Service Control handle");
SvcHandle = NULL;
*FServiceName = "";
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Start() {
if (!StartService(SvcHandle, 0, NULL)) {
DWORD res = GetLastError();
ThrowException(res);
}
else {
QueryStatus();
FStatus = MapState(Status->dwCurrentState);
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Stop() {
if (!ControlService(SvcHandle, SERVICE_CONTROL_STOP, Status)) {
DWORD res = GetLastError();
ThrowException(res);
}
else
FStatus = MapState(Status->dwCurrentState);
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Pause() {
if (!ControlService(SvcHandle, SERVICE_CONTROL_PAUSE, Status)) {
DWORD res = GetLastError();
ThrowException(res);
}
else
FStatus = MapState(Status->dwCurrentState);
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Continue() {
if (!ControlService(SvcHandle, SERVICE_CONTROL_CONTINUE, Status)) {
DWORD res = GetLastError();
ThrowException(res);
}
else
FStatus = MapState(Status->dwCurrentState);
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::ThrowException(int i) {
switch (i) {
case 0:
break;
case ERROR_FILE_NOT_FOUND:
throw Exception("File not found");
break;
case ERROR_ACCESS_DENIED:
throw Exception("Access denied");
break;
case ERROR_INVALID_HANDLE:
throw Exception("Invalid handle");
break;
case ERROR_INVALID_NAME:
throw Exception("Invalid name");
break;
case ERROR_SERVICE_DOES_NOT_EXIST:
throw Exception("Service does not exist");
break;
case ERROR_PATH_NOT_FOUND:
throw Exception("Path not found");
break;
case ERROR_INVALID_PARAMETER:
throw Exception("Invalid parameter");
break;
case ERROR_SERVICE_ALREADY_RUNNING:
throw Exception("Service already running");
break;
case ERROR_SERVICE_DATABASE_LOCKED:
throw Exception("Service database locked");
break;
case ERROR_SERVICE_DEPENDENCY_DELETED:
throw Exception("Dependency deleted");
break;
case ERROR_SERVICE_DEPENDENCY_FAIL:
throw Exception("Dependency failed");
break;
case ERROR_SERVICE_DISABLED:
throw Exception("Service disabled");
break;
case ERROR_SERVICE_LOGON_FAILED:
throw Exception("Service logon failed");
break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
throw Exception("Service marked for deletion");
break;
case ERROR_SERVICE_NO_THREAD:
throw Exception("No thread for service");
break;
case ERROR_DEPENDENT_SERVICES_RUNNING:
throw Exception("Dependent services running");
break;
case ERROR_INVALID_SERVICE_CONTROL:
throw Exception("Invalid service control");
break;
case ERROR_SERVICE_CANNOT_ACCEPT_CTRL:
throw Exception("Not ready for service control");
break;
case ERROR_SERVICE_NOT_ACTIVE:
throw Exception("Service not active");
break;
case ERROR_SERVICE_REQUEST_TIMEOUT:
throw Exception("Service request timed out");
break;
default:
throw Exception("Unexpected service error: " + IntToStr(i));
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::QueryStatus() {
if (!QueryServiceStatus(SvcHandle, Status)) {
DWORD res = GetLastError();
ThrowException(res);
}
else
FStatus = MapState(Status->dwCurrentState);
}
//---------------------------------------------------------------------------
AnsiString __fastcall TServiceController::GetServiceName() {
return *FServiceName;
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::SetServiceName(AnsiString sn) {
*FServiceName = sn;
}
//---------------------------------------------------------------------------
ServiceState __fastcall TServiceController::GetStatus() {
QueryStatus();
return FStatus;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TServiceController::GetStatusString() {
QueryStatus();
return *FStatusString;
}
//---------------------------------------------------------------------------
ServiceState __fastcall TServiceController::MapState(DWORD i) {
ServiceState r;
switch (i) {
case SERVICE_STOPPED:
r = Stopped;
break;
case SERVICE_START_PENDING:
r = Starting;
break;
case SERVICE_STOP_PENDING:
r = Stopping;
break;
case SERVICE_RUNNING:
r = Running;
break;
case SERVICE_CONTINUE_PENDING:
r = Continuing;
break;
case SERVICE_PAUSE_PENDING:
r = Pausing;
break;
case SERVICE_PAUSED:
r = Paused;
break;
default:
r = None;
}
switch (r) {
case Stopped:
*FStatusString = "Stopped";
break;
case Starting:
*FStatusString = "Starting";
break;
case Stopping:
*FStatusString = "Stopping";
break;
case Running:
*FStatusString = "Running";
break;
case Continuing:
*FStatusString = "Continuing";
break;
case Pausing:
*FStatusString = "Pausing";
break;
case Paused:
*FStatusString = "Paused";
break;
default:
*FStatusString = "None";
}
return r;
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Install(AnsiString SvcName, AnsiString DispName, StartType st, AnsiString FilePath, bool Interactive) {
SC_HANDLE hand;
if (!CanInstall) {
if (SvcHandle != NULL)
CloseServ();
CloseManager();
OpenManager(true);
}
else {
if (SvcHandle != NULL)
CloseServ();
}
DWORD desAccess = SERVICE_WIN32_OWN_PROCESS;
if (Interactive)
desAccess |= SERVICE_INTERACTIVE_PROCESS;
hand = CreateService(ManHandle, SvcName.c_str(), DispName.c_str(),
SERVICE_ALL_ACCESS & SERVICE_QUERY_STATUS & DELETE & GENERIC_READ & GENERIC_WRITE & GENERIC_EXECUTE,
desAccess, st, SERVICE_ERROR_IGNORE,
FilePath.c_str(), NULL, NULL, NULL, NULL, NULL);
if (hand != NULL) {
if (SvcHandle != NULL)
CloseServ();
SvcHandle = hand;
CloseManager();
OpenManager(false);
OpenServ(SvcName);
}
else {
DWORD res = GetLastError();
CloseManager();
ThrowException(res);
}
}
//---------------------------------------------------------------------------
void __fastcall TServiceController::Uninstall() {
AnsiString *OldService = new AnsiString();
if (!CanInstall) {
if (SvcHandle != NULL) {
*OldService = *FServiceName;
CloseServ();
}
CloseManager();
OpenManager(true);
OpenServ(*OldService);
if (MapState(Status->dwCurrentState) != Stopped)
Stop();
}
if (SvcHandle != NULL) {
if (!DeleteService(SvcHandle)) {
DWORD res = GetLastError();
ThrowException(res);
}
else
CloseServ();
}
delete OldService;
}
//---------------------------------------------------------------------------