-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathservice.pas
220 lines (200 loc) · 6.91 KB
/
service.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
// Windows Service Control Manager access code based on Silvio Iaccarino code available at:
// http://www.iaccarino.de/silvio/winstuff.htm
// function IsServiceInstalled(ServiceName: string): boolean;
// function IsServiceRunning(ServiceName: string): boolean;
// function InstallService(FileName, ServiceName, DisplayName, Description: string; ServiceType, StartType: cardinal; AccountName, AccountPassword: string): boolean;
// function RemoveService(ServiceName: string): boolean;
// function StartService(ServiceName: string): boolean;
// function StopService(ServiceName: string): boolean;
// function SetupService(service, port, comment: string): boolean;
type
SERVICE_STATUS = record
dwServiceType : cardinal;
dwCurrentState : cardinal;
dwControlsAccepted : cardinal;
dwWin32ExitCode : cardinal;
dwServiceSpecificExitCode : cardinal;
dwCheckPoint : cardinal;
dwWaitHint : cardinal;
end;
HANDLE = cardinal;
const
SERVICE_QUERY_CONFIG = $1;
SERVICE_CHANGE_CONFIG = $2;
SERVICE_QUERY_STATUS = $4;
SERVICE_START = $10;
SERVICE_STOP = $20;
SERVICE_ALL_ACCESS = $f01ff;
SC_MANAGER_ALL_ACCESS = $f003f;
SERVICE_WIN32_OWN_PROCESS = $10;
SERVICE_WIN32_SHARE_PROCESS = $20;
SERVICE_WIN32 = $30;
SERVICE_INTERACTIVE_PROCESS = $100;
SERVICE_BOOT_START = $0;
SERVICE_SYSTEM_START = $1;
SERVICE_AUTO_START = $2;
SERVICE_DEMAND_START = $3;
SERVICE_DISABLED = $4;
SERVICE_DELETE = $10000;
SERVICE_CONTROL_STOP = $1;
SERVICE_CONTROL_PAUSE = $2;
SERVICE_CONTROL_CONTINUE = $3;
SERVICE_CONTROL_INTERROGATE = $4;
SERVICE_STOPPED = $1;
SERVICE_START_PENDING = $2;
SERVICE_STOP_PENDING = $3;
SERVICE_RUNNING = $4;
SERVICE_CONTINUE_PENDING = $5;
SERVICE_PAUSE_PENDING = $6;
SERVICE_PAUSED = $7;
function OpenSCManager(lpMachineName, lpDatabaseName: string; dwDesiredAccess: cardinal): HANDLE;
external '[email protected] stdcall';
function OpenService(hSCManager: HANDLE; lpServiceName: string; dwDesiredAccess: cardinal): HANDLE;
external '[email protected] stdcall';
function CloseServiceHandle(hSCObject: HANDLE): boolean;
external '[email protected] stdcall';
function CreateService(hSCManager: HANDLE; lpServiceName, lpDisplayName: string; dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl: cardinal; lpBinaryPathName, lpLoadOrderGroup: string; lpdwTagId: cardinal; lpDependencies, lpServiceStartName, lpPassword: string): cardinal;
external '[email protected] stdcall';
function DeleteService(hService: HANDLE): boolean;
external '[email protected] stdcall';
function StartNTService(hService: HANDLE; dwNumServiceArgs: cardinal; lpServiceArgVectors: cardinal): boolean;
external '[email protected] stdcall';
function ControlService(hService: HANDLE; dwControl: cardinal; var ServiceStatus: SERVICE_STATUS): boolean;
external '[email protected] stdcall';
function QueryServiceStatus(hService: HANDLE; var ServiceStatus: SERVICE_STATUS): boolean;
external '[email protected] stdcall';
function QueryServiceStatusEx(hService: HANDLE; ServiceStatus: SERVICE_STATUS): boolean;
external '[email protected] stdcall';
function GetLastError(): cardinal;
external '[email protected] stdcall';
function OpenServiceManager(): HANDLE;
begin
if UsingWinNT() then
begin
Result := OpenSCManager('', '', SC_MANAGER_ALL_ACCESS);
if Result = 0 then
MsgBox('the service control manager is not available', mbError, MB_OK)
end
else
begin
MsgBox('only NT based systems support services', mbError, MB_OK)
Result := 0;
end
end;
function IsServiceInstalled(ServiceName: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
hService := OpenService(hSCM, ServiceName, SERVICE_QUERY_CONFIG);
if hService <> 0 then
begin
Result := true;
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end
end;
function InstallService(FileName, ServiceName, DisplayName, Description: string; ServiceType, StartType: cardinal; AccountName, AccountPassword: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
ServiceStartName: string;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
if AccountName <> '' then
ServiceStartName := '.\' + AccountName;
hService := CreateService(hSCM, ServiceName, DisplayName, SERVICE_ALL_ACCESS, ServiceType, StartType, 0, FileName, '', 0, '', ServiceStartName, AccountPassword);
if hService <> 0 then
begin
Result := true;
// Win2K & WinXP supports aditional description text for services
if Description<> '' then
RegWriteStringValue(HKLM, 'System\CurrentControlSet\Services\' + ServiceName, 'Description', Description);
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end
end;
function RemoveService(ServiceName: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
hService := OpenService(hSCM, ServiceName, SERVICE_DELETE);
if hService <> 0 then
begin
Result := DeleteService(hService);
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end
end;
function StartService(ServiceName: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
hService := OpenService(hSCM, ServiceName, SERVICE_START);
if hService <> 0 then
begin
Result := StartNTService(hService, 0, 0);
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end;
end;
function StopService(ServiceName: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
hService := OpenService(hSCM, ServiceName, SERVICE_STOP);
if hService <> 0 then
begin
Result := ControlService(hService, SERVICE_CONTROL_STOP, Status);
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end;
end;
function IsServiceRunning(ServiceName: string): boolean;
var
hSCM : HANDLE;
hService: HANDLE;
Status : SERVICE_STATUS;
begin
hSCM := OpenServiceManager();
Result := false;
if hSCM <> 0 then
begin
hService := OpenService(hSCM, ServiceName, SERVICE_QUERY_STATUS);
if hService <> 0 then
begin
if QueryServiceStatus(hService, Status) then
Result := (Status.dwCurrentState = SERVICE_RUNNING);
CloseServiceHandle(hService)
end;
CloseServiceHandle(hSCM)
end
end;