-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceTools.pas
More file actions
474 lines (430 loc) · 11.9 KB
/
ServiceTools.pas
File metadata and controls
474 lines (430 loc) · 11.9 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
unit ServiceTools;
{$WARN SYMBOL_PLATFORM OFF}
{$R+}
interface
uses
Windows,WinSvc;
type
TServiceStartType =
(sstBoot, // SERVICE_BOOT_START
sstSystem, // SERVICE_SYSTEM_START
sstAuto, // SERVICE_AUTO_START
sstDemand, // SERVICE_DEMAND_START
sstDisabled); // SERVICE_DISABLED
function ServiceExists( sMachine, sService : string): boolean;
function ServiceStart( sMachine, sService : string): boolean;
function ServiceStop(sMachine, sService : string): boolean;
function ServiceRunning(sMachine, sService : string): boolean;
function ServiceUpdateAccount(sMachine, sService,UserName,Password : string; StartType: TServiceStartType): boolean;
function GetServiceStartType(sMachine, sService: string): TServiceStartType;
function SetServiceStartType(sMachine, sService: string; StartType: TServiceStartType): boolean;
function GetServiceUser(sMachine, sService: string): String;
implementation
//
// start service
//
// return TRUE if successful
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
// sService
// service name, ie: Alerter
//
function ServiceStart(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// temp char pointer
psTemp : PChar;
//
// check point
dwChkP : DWord;
begin
ss.dwCurrentState := 0;
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
// open a handle to
// the specified service
schs := OpenService(
schm,
PChar(sService),
// we want to
// start the service and
SERVICE_START or
// query service status
SERVICE_QUERY_STATUS);
// if successful...
if(schs > 0)then
begin
psTemp := Nil;
if(StartService(
schs,
0,
psTemp))then
begin
// check status
if(QueryServiceStatus(
schs,
ss))then
begin
while(SERVICE_RUNNING
<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;
//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;
if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;
// close service handle
CloseServiceHandle(schs);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
// return TRUE if
// the service status is running
Result :=
SERVICE_RUNNING =
ss.dwCurrentState;
end;
function ServiceStop(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// check point
dwChkP : DWord;
begin
// connect to the service
// control manager
schm := OpenSCManager(
PChar(sMachine),
Nil,
SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
// open a handle to
// the specified service
schs := OpenService(
schm,
PChar(sService),
// we want to
// stop the service and
SERVICE_STOP or
// query service status
SERVICE_QUERY_STATUS);
// if successful...
if(schs > 0)then
begin
if(ControlService(
schs,
SERVICE_CONTROL_STOP,
ss))then
begin
// check status
if(QueryServiceStatus(
schs,
ss))then
begin
while(SERVICE_STOPPED
<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;
//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;
if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;
// close service handle
CloseServiceHandle(schs);
end;
// close service control
// manager handle
CloseServiceHandle(schm);
end;
// return TRUE if
// the service status is stopped
Result :=
SERVICE_STOPPED =
ss.dwCurrentState;
end;
function ServiceRunning(sMachine, sService : string): boolean;
var
SCManHandle, SvcHandle: SC_Handle;
SS: TServiceStatus;
dwStat: DWORD;
begin
dwStat := 0;
// Open service manager handle.
SCManHandle := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
if (SCManHandle > 0) then
begin
SvcHandle := OpenService(SCManHandle, PChar(sService), SERVICE_QUERY_STATUS);
// if Service installed
if (SvcHandle > 0) then
begin
// SS structure holds the service status (TServiceStatus);
if (QueryServiceStatus(SvcHandle, SS)) then
dwStat := ss.dwCurrentState;
CloseServiceHandle(SvcHandle);
end;
CloseServiceHandle(SCManHandle);
end;
Result := dwStat = SERVICE_RUNNING
end;
function ServiceExists( sMachine, sService : string): boolean;
var
SCManHandle, SvcHandle: SC_Handle;
begin
Result:=False;
// Open service manager handle.
SCManHandle := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
if (SCManHandle > 0) then
begin
SvcHandle := OpenService(SCManHandle, PChar(sService), SERVICE_QUERY_STATUS);
// if Service installed
Result:= (SvcHandle > 0);
CloseServiceHandle(SCManHandle);
end;
end;
function ServiceUpdateAccount(sMachine, sService,UserName,Password : string; StartType: TServiceStartType): boolean;
var
Ret: BOOL;
SvcMgr, Svc: SC_HANDLE;
BytesNeeded: DWORD;
PQrySvcCnfg: LPQUERY_SERVICE_CONFIG;
begin
Result := False;
SvcMgr := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr <> 0 then
begin
Svc := OpenService(SvcMgr, PChar(sService), STANDARD_RIGHTS_REQUIRED or SERVICE_CHANGE_CONFIG or SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if Svc <> 0 then
begin
PQrySvcCnfg := nil;
BytesNeeded := 4096;
repeat
ReallocMem(PQrySvcCnfg, BytesNeeded);
Ret := QueryServiceConfig(Svc, PQrySvcCnfg, BytesNeeded, BytesNeeded);
until Ret or (GetLastError <> ERROR_INSUFFICIENT_BUFFER);
PQrySvcCnfg^.lpServiceStartName:=PChar(UserName);
PQrySvcCnfg^.dwStartType:=Ord(StartType);
Result := ChangeServiceConfig(Svc,
PQrySvcCnfg^.dwServiceType,
PQrySvcCnfg^.dwStartType,
PQrySvcCnfg^.dwErrorControl,
nil, {PQrySvcCnfg^.lpBinaryPathName,}
nil, {PQrySvcCnfg^.lpLoadOrderGroup,}
nil, {PQrySvcCnfg^.dwTagId,}
nil, {PQrySvcCnfg^.lpDependencies,}
PQrySvcCnfg^.lpServiceStartName,
PChar(Password), {password-write only-not readable}
nil {PQrySvcCnfg^.lpDisplayName} );
CloseServiceHandle(Svc);
end;
CloseServiceHandle(SvcMgr);
end;
end;
function GetServiceStartType(sMachine, sService: string): TServiceStartType;
var
Ret: BOOL;
SvcMgr, Svc: SC_HANDLE;
BytesNeeded: DWORD;
PQrySvcCnfg: LPQUERY_SERVICE_CONFIG;
begin
SvcMgr := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr <> 0 then
begin
Svc := OpenService(SvcMgr, PChar(sService), STANDARD_RIGHTS_REQUIRED or SERVICE_CHANGE_CONFIG or SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if Svc <> 0 then
begin
PQrySvcCnfg := nil;
BytesNeeded := 4096;
repeat
ReallocMem(PQrySvcCnfg, BytesNeeded);
Ret := QueryServiceConfig(Svc, PQrySvcCnfg, BytesNeeded, BytesNeeded);
until Ret or (GetLastError <> ERROR_INSUFFICIENT_BUFFER);
Result := TServiceStartType(PQrySvcCnfg^.dwStartType);
CloseServiceHandle(Svc);
end;
CloseServiceHandle(SvcMgr);
end;
end;
function GetServiceUser(sMachine, sService: string): String;
var
Ret: BOOL;
SvcMgr, Svc: SC_HANDLE;
BytesNeeded: DWORD;
PQrySvcCnfg: LPQUERY_SERVICE_CONFIG;
begin
SvcMgr := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr <> 0 then
begin
Svc := OpenService(SvcMgr, PChar(sService), STANDARD_RIGHTS_REQUIRED or SERVICE_CHANGE_CONFIG or SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if Svc <> 0 then
begin
PQrySvcCnfg := nil;
BytesNeeded := 4096;
repeat
ReallocMem(PQrySvcCnfg, BytesNeeded);
Ret := QueryServiceConfig(Svc, PQrySvcCnfg, BytesNeeded, BytesNeeded);
until Ret or (GetLastError <> ERROR_INSUFFICIENT_BUFFER);
Result := PQrySvcCnfg^.lpServiceStartName;
CloseServiceHandle(Svc);
end;
CloseServiceHandle(SvcMgr);
end;
end;
function SetServiceStartType(sMachine, sService: string; StartType: TServiceStartType):Boolean;
var
Ret: BOOL;
SvcMgr, Svc: SC_HANDLE;
BytesNeeded: DWORD;
PQrySvcCnfg: LPQUERY_SERVICE_CONFIG;
begin
Result := False;
SvcMgr := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_ALL_ACCESS);
if SvcMgr <> 0 then
begin
Svc := OpenService(SvcMgr, PChar(sService), STANDARD_RIGHTS_REQUIRED or SERVICE_CHANGE_CONFIG or SERVICE_QUERY_STATUS or SERVICE_QUERY_CONFIG);
if Svc <> 0 then
begin
PQrySvcCnfg := nil;
BytesNeeded := 4096;
repeat
ReallocMem(PQrySvcCnfg, BytesNeeded);
Ret := QueryServiceConfig(Svc, PQrySvcCnfg, BytesNeeded, BytesNeeded);
until Ret or (GetLastError <> ERROR_INSUFFICIENT_BUFFER);
PQrySvcCnfg^.dwStartType:=Ord(StartType);
Result := ChangeServiceConfig(Svc,
PQrySvcCnfg^.dwServiceType,
PQrySvcCnfg^.dwStartType,
PQrySvcCnfg^.dwErrorControl,
nil, {PQrySvcCnfg^.lpBinaryPathName,}
nil, {PQrySvcCnfg^.lpLoadOrderGroup,}
nil, {PQrySvcCnfg^.dwTagId,}
nil, {PQrySvcCnfg^.lpDependencies,}
nil, {PQrySvcCnfg^.lpServiceStartName,}
nil, {password-write only-not readable}
nil {PQrySvcCnfg^.lpDisplayName} );
CloseServiceHandle(Svc);
end;
CloseServiceHandle(SvcMgr);
end;
end;
end.