-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTMSrvCtrl.pas
299 lines (253 loc) · 8.19 KB
/
TMSrvCtrl.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
unit TMSrvCtrl;
{
Aestan Tray Menu
Made by Onno Broekmans; visit http://www.xs4all.nl/~broekroo/aetraymenu
for more information.
This work is hereby released into the Public Domain. To view a copy of the
public domain dedication, visit:
http://creativecommons.org/licenses/publicdomain/
or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
California 94305, USA.
This unit contains the TTMService class, which can be used to control
Windows services.
}
{
NOTE:
This unit is a replacement of the TSrvCtrl component, which was originally
used by AeTrayMenu. However, this component had a lot of features ATM
didn't need, and most of all, TSrvCtrl was only for non-commercial use.
Some of this code has been inspired by the TSrvCtrl component, though,
and therefore I'd like to thank the original authors:
* GSC (www.gsc.hu)
* TMEDIA (www.tmedia.de)
>>PLEASE DO NOT REMOVE THIS NOTE<<
}
interface
uses
Windows, SysUtils, Classes, WinSvc;
type
{ Indicates the status of the service }
TServiceState = (svsStopped = $00000001, svsStartPending, svsStopPending,
svsRunning, svsContinuePending, svsPausePending, svsPaused);
TServiceControl = (svcStop, svcPauseContinue);
TServiceControls = set of TServiceControl;
EServiceException = class(Exception)
public
constructor Create(AErrorCode: Integer); overload;
end;
{ This class can control Windows services.
Note: if you make calls to methods or use properties that need a handle to
the service (in other words: that need TTMService to be activated),
while TTMService hasn't been activated (yet), an exception will be raised. }
TTMService = class
private
FServiceName: String;
FServiceHandle: SC_HANDLE;
FSCManagerHandle: SC_HANDLE;
procedure SetServiceName(const Value: String);
function GetDisplayName: String;
function GetState: TServiceState;
function GetActive: Boolean;
procedure SetActive(const Value: Boolean);
function GetControlsAccepted: TServiceControls;
protected
property SCManagerHandle: SC_HANDLE read FSCManagerHandle;
property ServiceHandle: SC_HANDLE read FServiceHandle;
procedure ControlService(ControlCode: DWORD); virtual;
procedure GetServiceConfig(var Config: TQueryServiceConfig);
procedure GetServiceStatus(var Status: TServiceStatus);
procedure VerifyActive;
public
{ Read to determine whether TTMService has an active/open handle to
the service.
Setting Active to true does the same as calling Open; setting to false
is equivalent to calling Close. }
property Active: Boolean read GetActive write SetActive;
{ Can be used to see if the service accepts a specific control code/action }
property ControlsAccepted: TServiceControls read GetControlsAccepted;
{ Can be used to retrieve the "display name" of the service }
property DisplayName: String read GetDisplayName;
{ Can be used to retrieve the status of the service }
property State: TServiceState read GetState;
{ The name of the service (*not* the display name). If TTMService has
already been activated/opened, it will first disconnect and then
store the property. You always have to activate the TTMService yourself,
since this will *not* be done for you automatically. }
property ServiceName: String read FServiceName write SetServiceName;
{ Opens a handle to the specified service. If TTMService has already been
activated, Close is called first, and then the handle is opened. }
procedure Open; virtual;
{ Closes the handle to the service. If TTMService is inactive, nothing
is done. }
procedure Close; virtual;
{ Starts the service. }
procedure Start; virtual;
{ Continues a paused service.}
procedure Continue; virtual;
{ Stops the service. }
procedure Stop; virtual;
{ Suspends/pauses the service. }
procedure Pause; virtual;
constructor Create; virtual;
destructor Destroy; override;
end; //ttmservice
implementation
uses
Forms;
resourcestring
SNoServiceOpen = 'No service has been opened yet!';
{ EServiceException }
constructor EServiceException.Create(AErrorCode: Integer);
begin
inherited Create(SysErrorMessage(AErrorCode));
end;
{ TTMService }
procedure TTMService.Close;
begin
VerifyActive;
if not CloseServiceHandle(ServiceHandle) then
raise EServiceException.Create(GetLastError);
FServiceHandle := 0;
if not CloseServiceHandle(SCManagerHandle) then
raise EServiceException.Create(GetLastError);
FSCManagerHandle := 0;
end;
procedure TTMService.Continue;
begin
ControlService(SERVICE_CONTROL_CONTINUE);
end;
procedure TTMService.ControlService(ControlCode: Cardinal);
var
ServiceStatus: TServiceStatus;
begin
VerifyActive;
if not WinSvc.ControlService(ServiceHandle, ControlCode, ServiceStatus) then
raise EServiceException.Create(GetLastError);
end;
constructor TTMService.Create;
begin
inherited Create;
end;
destructor TTMService.Destroy;
begin
if Active then
Close;
inherited Destroy;
end;
function TTMService.GetActive: Boolean;
begin
Result := (SCManagerHandle <> 0) and (ServiceHandle <> 0);
end;
function TTMService.GetControlsAccepted: TServiceControls;
var
Status: TServiceStatus;
begin
GetServiceStatus(Status);
with Status do
begin
if (dwControlsAccepted and SERVICE_ACCEPT_STOP) = SERVICE_ACCEPT_STOP then
Include(Result, svcStop);
if (dwControlsAccepted and SERVICE_ACCEPT_PAUSE_CONTINUE) = SERVICE_ACCEPT_PAUSE_CONTINUE then
Include(Result, svcPauseContinue);
end; //with config
end;
function TTMService.GetDisplayName: String;
var
Config: TQueryServiceConfig;
begin
GetServiceConfig(Config);
Result := Config.lpDisplayName;
end;
procedure TTMService.GetServiceConfig(var Config: TQueryServiceConfig);
var
BufferSizeNeeded, ErrorCode: Cardinal;
Buffer: PQueryServiceConfig;
begin
VerifyActive;
Buffer := nil;
if not QueryServiceConfig(ServiceHandle, Buffer, 0, BufferSizeNeeded) then
begin
ErrorCode := GetLastError;
if ErrorCode = ERROR_INSUFFICIENT_BUFFER then
begin
GetMem(Buffer, BufferSizeNeeded);
try
if not QueryServiceConfig(ServiceHandle, Buffer, BufferSizeNeeded,
BufferSizeNeeded) then
raise EServiceException.Create(GetLastError);
Config := Buffer^;
finally
FreeMem(Buffer);
end; //try..finally
end
else //if errorcode = error_insufficient_buffer
raise EServiceException.Create(ErrorCode);
end; //if not queryserviceconfig(<empty buffer>)
end;
procedure TTMService.GetServiceStatus(var Status: TServiceStatus);
begin
VerifyActive;
if not QueryServiceStatus(ServiceHandle, Status) then
raise EServiceException.Create(GetLastError);
end;
function TTMService.GetState: TServiceState;
var
Status: TServiceStatus;
begin
GetServiceStatus(Status);
Result := TServiceState(Status.dwCurrentState);
end;
procedure TTMService.Open;
begin
if Active then
Close;
//Open the service control manager
FSCManagerHandle := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if FSCManagerHandle = 0 then
raise EServiceException.Create(GetLastError);
//Open the service
try
FServiceHandle := OpenService(SCManagerHandle, PChar(ServiceName),
SERVICE_PAUSE_CONTINUE or SERVICE_QUERY_CONFIG or SERVICE_QUERY_STATUS or
SERVICE_START or SERVICE_STOP);
if ServiceHandle = 0 then
raise EServiceException.Create(GetLastError);
except
//Clean up the service control manager handle
CloseServiceHandle(SCManagerHandle);
raise;
end;
end;
procedure TTMService.Pause;
begin
ControlService(SERVICE_CONTROL_PAUSE);
end;
procedure TTMService.SetActive(const Value: Boolean);
begin
if Value then
Open
else
Close;
end;
procedure TTMService.SetServiceName(const Value: String);
begin
FServiceName := Value;
end;
procedure TTMService.Start;
var
Parameters: PChar;
begin
Parameters := nil;
if not StartService(ServiceHandle, 0, Parameters) then
raise EServiceException.Create(GetLastError);
end;
procedure TTMService.Stop;
begin
ControlService(SERVICE_CONTROL_STOP);
end;
procedure TTMService.VerifyActive;
begin
if not Active then
raise EServiceException.Create(SNoServiceOpen);
end;
end.