-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaservices.pas
468 lines (419 loc) · 14 KB
/
caservices.pas
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
unit caServices;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
Windows,
SysUtils,
Classes,
Contnrs,
WinSvc;
type
//---------------------------------------------------------------------------
// TcaService
//---------------------------------------------------------------------------
TcaServiceStatus = (ssUndefined, ssStopped, ssStartPending, ssStopPending,
ssRunning, ssContinuePending, ssPausePending, ssPaused);
TcaUpdateCallback = procedure of object;
TcaService = class(TObject)
private
// private members...
FMachineName: string;
FServiceName: string;
FDisplayName: string;
FServiceStatus: TServiceStatus;
FUpdateCallback: TcaUpdateCallback;
// property methods...
function GetServiceStatus: TcaServiceStatus;
public
// lifetime...
constructor Create(const AMachineName, AServiceName, ADisplayName: string; AServiceStatus: TServiceStatus);
// public methods...
function Start: Boolean;
function Stop: Boolean;
// public properties...
property ServiceName: string read FServiceName;
property DisplayName: string read FDisplayName;
property ServiceStatus: TcaServiceStatus read GetServiceStatus;
property UpdateCallback: TcaUpdateCallback read FUpdateCallback write FUpdateCallback;
end;
//---------------------------------------------------------------------------
// IcaServiceList
//---------------------------------------------------------------------------
IcaServiceList = interface
['{0F9F855C-A80B-40AB-A114-173FC2581EA1}']
// property methods...
function GetIgnoreInactive: Boolean;
function GetMachine: string;
function GetService(Index: Integer): TcaService;
function GetServiceCount: Integer;
function GetServiceType: DWORD;
procedure SetIgnoreInactive(const Value: Boolean);
procedure SetMachine(const Value: string);
procedure SetServiceType(Value: DWORD);
// interface methods - IcaServiceList...
function FindServiceName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
function FindDisplayName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
procedure GetServiceNamesAsStrings(AList: TStrings);
procedure SortByDisplayName;
procedure Update;
// interface properties - IcaServiceList...
property IgnoreInactive: Boolean read GetIgnoreInactive write SetIgnoreInactive;
property Machine: string read GetMachine write SetMachine;
property ServiceCount: Integer read GetServiceCount;
property Services[Index: Integer]: TcaService read GetService; default;
property ServiceType: DWORD read GetServiceType write SetServiceType;
end;
//---------------------------------------------------------------------------
// TcaServiceList
//---------------------------------------------------------------------------
TcaServiceList = class(TInterfacedObject, IcaServiceList)
private
// Private fields
FList: TObjectList;
// property fields...
FIgnoreInactive: Boolean;
FMachineName: string;
FServiceType: DWORD;
// private methods...
function FindService(const ASearchName: string; AllowPartialMatch, UseServiceName: Boolean): TcaService;
procedure BuildList;
procedure Initialize;
// property methods...
function GetIgnoreInactive: Boolean;
function GetMachine: string;
function GetService(Index: Integer): TcaService;
function GetServiceCount: Integer;
function GetServiceType: DWORD;
procedure SetIgnoreInactive(const Value: Boolean);
procedure SetMachine(const Value: string);
procedure SetServiceType(Value: DWORD);
protected
// interface methods - IcaServiceList...
function FindServiceName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
function FindDisplayName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
procedure GetServiceNamesAsStrings(AList: TStrings);
procedure SortByDisplayName;
procedure Update;
public
// lifetime...
constructor Create; overload;
constructor Create(const AMachineName: string); overload;
destructor Destroy; override;
end;
implementation
//---------------------------------------------------------------------------
// TcaService
//---------------------------------------------------------------------------
// lifetime...
constructor TcaService.Create(const AMachineName, AServiceName, ADisplayName: string; AServiceStatus: TServiceStatus);
begin
inherited Create;
FMachineName := AMachineName;
FServiceName := AServiceName;
FDisplayName := ADisplayName;
FServiceStatus := AServiceStatus;
end;
// public methods...
function TcaService.Start: Boolean;
var
ServiceControlManager: SC_HANDLE;
ServiceHandle: SC_HANDLE;
Flags: Cardinal;
ArgVectors: PChar;
ServStatus: TServiceStatus;
CheckPoint: DWORD;
Started: Boolean;
begin
ServStatus.dwCurrentState := Cardinal(-1);
Started := False;
ServiceControlManager := OpenSCManager(PChar(FMachineName), nil, SC_MANAGER_ALL_ACCESS);
if ServiceControlManager <> 0 then
begin
Flags := SERVICE_START or SERVICE_QUERY_STATUS;
ServiceHandle := OpenService(ServiceControlManager, PChar(FServiceName), Flags);
if ServiceHandle <> 0 then
begin
ArgVectors := nil;
if StartService(ServiceHandle, 0, ArgVectors) then
begin
Started := True;
if QueryServiceStatus(ServiceHandle, ServStatus) then
begin
while (ServStatus.dwCurrentState <> SERVICE_RUNNING) and
(ServStatus.dwCurrentState <> SERVICE_STOPPED) do
begin
CheckPoint := ServStatus.dwCheckPoint;
Sleep(ServStatus.dwWaitHint);
if not QueryServiceStatus(ServiceHandle, ServStatus) then
// couldn't check status...
Break;
if ServStatus.dwCheckPoint < CheckPoint then
// avoid infinite loop...
Break;
end;
end;
end;
CloseServiceHandle(ServiceHandle);
end;
CloseServiceHandle(ServiceControlManager);
end;
Result := (ServStatus.dwCurrentState = SERVICE_RUNNING) or
((ServStatus.dwCurrentState = SERVICE_STOPPED) and Started);
if Assigned(FUpdateCallback) then
FUpdateCallback;
end;
function TcaService.Stop: Boolean;
var
ServiceControlManager: SC_HANDLE;
ServiceHandle: SC_HANDLE;
Flags: Cardinal;
ServStatus: TServiceStatus;
CheckPoint: DWORD;
begin
ServStatus.dwCurrentState := Cardinal(-1);
ServiceControlManager := OpenSCManager(PChar(FMachineName), nil, SC_MANAGER_ALL_ACCESS);
if ServiceControlManager <> 0 then
begin
Flags := SERVICE_STOP or SERVICE_QUERY_STATUS;
ServiceHandle := OpenService(ServiceControlManager, PChar(FServiceName), Flags);
if ServiceHandle <> 0 then
begin
if ControlService(ServiceHandle, SERVICE_CONTROL_STOP, ServStatus) then
begin
if QueryServiceStatus(ServiceHandle, ServStatus) then
begin
while ServStatus.dwCurrentState <> SERVICE_STOPPED do
begin
CheckPoint := ServStatus.dwCheckPoint;
Sleep(ServStatus.dwWaitHint);
if not QueryServiceStatus(ServiceHandle, ServStatus) then
// couldn't check status...
Break;
if ServStatus.dwCheckPoint < CheckPoint then
// avoid infinite loop...
Break;
end;
end;
end;
CloseServiceHandle(ServiceHandle);
end;
CloseServiceHandle(ServiceControlManager);
end;
Result := ServStatus.dwCurrentState = SERVICE_STOPPED;
if Assigned(FUpdateCallback) then
FUpdateCallback;
end;
// property methods...
function TcaService.GetServiceStatus: TcaServiceStatus;
begin
case FServiceStatus.dwCurrentState of
SERVICE_STOPPED: Result := ssStopped;
SERVICE_START_PENDING: Result := ssStartPending;
SERVICE_STOP_PENDING: Result := ssStopPending;
SERVICE_RUNNING: Result := ssRunning;
SERVICE_CONTINUE_PENDING: Result := ssContinuePending;
SERVICE_PAUSE_PENDING: Result := ssPausePending;
SERVICE_PAUSED: Result := ssPaused;
else
Result := ssUndefined;
end;
end;
{
SERVICE_STOPPED = $00000001;
SERVICE_START_PENDING = $00000002;
SERVICE_STOP_PENDING = $00000003;
SERVICE_RUNNING = $00000004;
SERVICE_CONTINUE_PENDING = $00000005;
SERVICE_PAUSE_PENDING = $00000006;
SERVICE_PAUSED = $00000007;
}
//---------------------------------------------------------------------------
// TcaServiceList
//---------------------------------------------------------------------------
// lifetime...
constructor TcaServiceList.Create;
begin
inherited;
Initialize;
Update;
end;
constructor TcaServiceList.Create(const AMachineName: string);
begin
inherited Create;
FMachineName := AMachineName;
Initialize;
Update;
end;
destructor TcaServiceList.Destroy;
begin
FList.Free;
inherited;
end;
// interface methods - IcaServiceList...
function TcaServiceList.FindDisplayName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
begin
Result := FindService(AServiceName, AllowPartialMatch, False);
end;
function TcaServiceList.FindServiceName(const AServiceName: string; AllowPartialMatch: Boolean): TcaService;
begin
Result := FindService(AServiceName, AllowPartialMatch, True);
end;
procedure TcaServiceList.GetServiceNamesAsStrings(AList: TStrings);
var
Index: Integer;
Service: TcaService;
ListEntry: String;
begin
AList.Clear;
AList.BeginUpdate;
try
for Index := 0 to Pred(GetServiceCount) do
begin
Service := GetService(Index);
ListEntry := Service.ServiceName;
AList.Add(ListEntry);
end;
finally
AList.EndUpdate;
end;
end;
function CompareDisplayNames(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TcaService(Item1).DisplayName, TcaService(Item2).DisplayName);
end;
procedure TcaServiceList.SortByDisplayName;
begin
FList.Sort(@CompareDisplayNames);
end;
procedure TcaServiceList.Update;
begin
BuildList;
end;
// private methods...
function TcaServiceList.FindService(const ASearchName: string; AllowPartialMatch, UseServiceName: Boolean): TcaService;
var
Ptr: Pointer;
Service: TcaService;
Name: string;
Matched: Boolean;
begin
Result := nil;
for Ptr in FList do
begin
Service := TcaService(Ptr);
if UseServiceName then
Name := Service.ServiceName
else
Name := Service.DisplayName;
if AllowPartialMatch then
Matched := Pos(AnsiLowerCase(ASearchName), AnsiLowerCase(Name)) > 0
else
Matched := AnsiLowerCase(ASearchName) = AnsiLowerCase(Name);
if Matched then
begin
Result := Service;
Break;
end;
end;
end;
procedure TcaServiceList.BuildList;
const
MAX_SERVICE_COUNT = 4096;
type
TServiceArray = array[0..Pred(MAX_SERVICE_COUNT)] of TEnumServiceStatus;
PServiceArray = ^TServiceArray;
var
Index: Integer;
ServiceControlManager: SC_HANDLE;
ServiceState: DWORD;
BytesNeeded: DWORD;
ServiceCount: DWORD;
ResumeHandle : DWord;
ServiceArray : PServiceArray;
Service: TcaService;
begin
FList.Clear;
if FIgnoreInactive then
ServiceState := SERVICE_ACTIVE
else
ServiceState := SERVICE_STATE_ALL;
// connect to the service control manager...
ServiceControlManager := OpenSCManager(PChar(FMachineName), nil, SC_MANAGER_ALL_ACCESS);
// if successful...
if ServiceControlManager <> 0 then
begin
ResumeHandle := 0;
New(ServiceArray);
EnumServicesStatus(ServiceControlManager,
FServiceType,
ServiceState,
ServiceArray^[0],
SizeOf(ServiceArray^),
BytesNeeded,
ServiceCount,
ResumeHandle );
for Index := 0 to Pred(ServiceCount) do
begin
Service := TcaService.Create(FMachineName,
ServiceArray^[Index].lpServiceName,
ServiceArray^[Index].lpDisplayName,
ServiceArray^[Index].ServiceStatus);
Service.UpdateCallback := Update;
FList.Add(Service);
end;
Dispose(ServiceArray);
CloseServiceHandle(ServiceControlManager);
end;
end;
procedure TcaServiceList.Initialize;
begin
FList := TObjectList.Create(True);
FServiceType := SERVICE_TYPE_ALL;
end;
// property methods...
function TcaServiceList.GetIgnoreInactive: Boolean;
begin
Result := FIgnoreInactive;
end;
function TcaServiceList.GetMachine: string;
begin
Result := FMachineName;
end;
function TcaServiceList.GetService(Index: Integer): TcaService;
begin
Result := TcaService(FList[Index]);
end;
function TcaServiceList.GetServiceCount: Integer;
begin
Result := FList.Count;
end;
function TcaServiceList.GetServiceType: DWORD;
begin
Result := FServiceType;
end;
procedure TcaServiceList.SetIgnoreInactive(const Value: Boolean);
begin
if Value <> FIgnoreInactive then
begin
FIgnoreInactive := Value;
Update;
end;
end;
procedure TcaServiceList.SetMachine(const Value: string);
begin
if Value <> FMachineName then
begin
FMachineName := Value;
Update;
end;
end;
procedure TcaServiceList.SetServiceType(Value: DWORD);
begin
if Value <> FServiceType then
begin
FServiceType := Value;
Update;
end;
end;
end.