-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservices.cpp
441 lines (376 loc) · 14.7 KB
/
services.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include "ErrorParser.h"
BOOL InstallServiceEx(HWND hWnd, SC_HANDLE schSCManager, char *scName, DWORD scStartType, char *szPath) {
SC_HANDLE schService;
// Ñúçäàâà ãî ñåðâèñà.
schService = CreateService(
schSCManager, // SCM áàçà ñ äàííè
scName, // èìå íà ñåðâèñà
scName, // èçëîæåíîòî èìå íà ñåðâèñà
SERVICE_ALL_ACCESS, // æåëàåù äîñòúï
SERVICE_KERNEL_DRIVER, // òèï íà ñåðâèñà
scStartType, // ñòàðòóâàù òèï
SERVICE_ERROR_NORMAL, // êîíòðîëåí òèï çà ãðåøêè
szPath, // ïúòå÷êà äî ñåðâèñà(*.sys)
NULL, // áåç çàðåæäàùà ãðóïà
NULL, // áåç ïðèáàâêà çà óñòàíîâÿâàíå
NULL, // áåç çàâèñèìîñòè
NULL, // ëîêàëåí îïèñ
NULL); // áåç ïàðîëà
if (schService == NULL) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
return FALSE;
}
MessageBox(hWnd, "Ñåðâèñúò å óñïåøíî èíñòàëèðàí", "FileHide", MB_ICONEXCLAMATION);
CloseServiceHandle(schService);
return TRUE;
}
BOOL DeleteServiceEx(HWND hWnd, SC_HANDLE schSCManager, char *szSvcName)
{
SC_HANDLE schService;
schService = OpenService(
schSCManager, // SCM áàçà ñ äàííè
szSvcName, // èìå íà ñåðâèñà
DELETE); // äîñòúï çà èçòðèâàíå
if (schService == NULL) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
return FALSE;
}
// Èçòðèâà ãî ñåðâèñà
if (!DeleteService(schService)) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
MessageBox(hWnd, "Ñåðâèñúò å óñïåøíî èçòðèåí", "FileHide", MB_ICONEXCLAMATION);
CloseServiceHandle(schService);
return TRUE;
}
BOOL StartServiceEx(HWND hWnd, SC_HANDLE schSCManager, char *szSvcName)
{
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
char str[50];
schService = OpenService(
schSCManager, // SCM áàçà ñ äàííè
szSvcName, // èìå íà ñåðâèñà
SERVICE_ALL_ACCESS); // ïúëåí äîñòúï
if (schService == NULL) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
return FALSE;
}
// Ïðîâåðêà íà ñúñòîÿíèåòî â ñëó÷àé, ÷å ñåðâèñà íå å ñïðÿë.
if (!QueryServiceStatusEx(
schService, // äðúæêà äî ñåðâèñà
SC_STATUS_PROCESS_INFO, // èíôîðìàöèîííî íèâî
(LPBYTE) &ssStatus, // àäðåñà íà ñòðóêòóðà
sizeof(SERVICE_STATUS_PROCESS), // ãîëåìèíà íà ñòðóêòóðà
&dwBytesNeeded)) // íåîáõîäèìà ãîëåìèíà, àêî áóôåðà å ìíîãî ìàëúê
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Ïðîâåðÿâà äàëè ñåðâèñà ðàáîòè.
if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING) {
MessageBox(hWnd, "Ñåðâèñúò íå ìîæå äà ñå ñòàðòóâà, çàùîòî å âå÷å ñòàðòóâàí è ðàáîòè", "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Ñïàñÿâàíå íà öúêàíèÿòà è ïúðâîíà÷àëíèÿ êîíòðîëíî-ïðîïóñêâàòåëåí ïóíêò.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Èç÷àêâàíå íà ñåðâèñà äà ñïðå ïðåäè äà ñå îïèòà äà ñòàðòóâà.
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
// Äà íå ñå ÷àêà ïî-äúëãî, îòêîëêîòî èç÷àêâàùèÿ íàìåê. Èíòåðâàëà å
// åäíà äåñåòà îò èç÷àêâàùèÿ íàìåê, íî íå ïîìàëêî îò åäíà ñåêóíäà
// è íå ïî-âå÷å îò 10 ñåêóíäè.
dwWaitTime = ssStatus.dwWaitHint / 10;
if(dwWaitTime < 1000)
dwWaitTime = 1000;
else if (dwWaitTime > 10000)
dwWaitTime = 10000;
Sleep(dwWaitTime);
// Ïðîâåðêà íà ñúñòîÿíèåòî, äîêàòî ñåðâèñà íå ñïðå â î÷àêâàíå.
if (!QueryServiceStatusEx(
schService, // äðúæêà äî ñåðâèñà
SC_STATUS_PROCESS_INFO, // èíôîðìàöèîííî íèâî
(LPBYTE) &ssStatus, // àäðåñà íà ñòðóêòóðà
sizeof(SERVICE_STATUS_PROCESS), // ãîëåìèíà íà ñòðóêòóðà
&dwBytesNeeded)) // íåîáõîäèìà ãîëåìèíà, àêî áóôåðà å ìíîãî ìàëúê
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
if (ssStatus.dwCheckPoint > dwOldCheckPoint) {
// Ïðîäúëæàâàíå íà èç÷àêâàíåòî è ïðîâåðêàòà.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
} else {
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) {
MessageBox(hWnd, "Âðåìåòî çà ÷àêàíå íà ñåðâèñà çà ñïèðàíå èçìèíà", "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
}
}
// Îïèò äà ñå ñòàðòóâà ñåðâèñà.
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Çàïî÷âà î÷àêâàíåòî...
// Ïðîâåðêà íà ñúñòîÿíèåòî, äîêàòî ñåðâèñà íå çàïî÷íå â î÷àêâàíå.
if (!QueryServiceStatusEx(
schService, // äðúæêà äî ñåðâèñà
SC_STATUS_PROCESS_INFO, // èíôîðìàöèîííî íèâî
(LPBYTE) &ssStatus, // àäðåñà íà ñòðóêòóðà
sizeof(SERVICE_STATUS_PROCESS), // ãîëåìèíà íà ñòðóêòóðà
&dwBytesNeeded)) // àêî áóôåðà å ìíîãî ìàëúê
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Ñïàñÿâàíå íà öúêàíèÿòà è ïúðâîíà÷àëíèÿ êîíòðîëíî-ïðîïóñêâàòåëåí ïóíêò.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING) {
dwWaitTime = ssStatus.dwWaitHint / 10;
if(dwWaitTime < 1000)
dwWaitTime = 1000;
else if (dwWaitTime > 10000)
dwWaitTime = 10000;
Sleep(dwWaitTime);
// Îòíîâî ïðîâåðêà íà ñúñòîÿíèåòî.
if (!QueryServiceStatusEx(
schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE) &ssStatus,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
break;
}
if (ssStatus.dwCheckPoint > dwOldCheckPoint) {
// Ïðîäúëæàâàíå íà èç÷àêâàíåòî è ïðîâåðêàòà.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
} else {
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) {
// Áåç íàïðåäúê â èç÷åêàùèÿ íàìåê.
break;
}
}
}
// Îïðåäåëÿ äàëè ñåðâèñà å ñòàðòóâàí.
if (ssStatus.dwCurrentState == SERVICE_RUNNING) {
MessageBox(hWnd, "Ñåðâèñúò å ñòàðòóâàí", "FileHide", MB_ICONEXCLAMATION);
CloseServiceHandle(schService);
return TRUE;
} else {
sprintf(str, "Ñåðâèñúò íå å ñòàðòóâàí.\n");
sprintf(str, "Òåêóùî ñúñòîÿíèå: %d\n", ssStatus.dwCurrentState);
sprintf(str, " Exit Code: %d\n", ssStatus.dwWin32ExitCode);
sprintf(str, " Check Point: %d\n", ssStatus.dwCheckPoint);
sprintf(str, " Wait Hint: %d\n", ssStatus.dwWaitHint);
MessageBox(hWnd, str, "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
}
BOOL StopDependentServices(SC_HANDLE hSCManager, SC_HANDLE hService);
BOOL StopServiceEx(HWND hWnd, SC_HANDLE schSCManager, char *szSvcName)
{
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssp;
DWORD dwStartTime = GetTickCount();
DWORD dwBytesNeeded;
DWORD dwTimeout = 30000; // 30 ñåêóíäíà ïàóçà
DWORD dwWaitTime;
schService = OpenService(schSCManager, szSvcName,
SERVICE_STOP | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS);
if (schService == NULL) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
return FALSE;
}
// Ïðîâåðÿâà äàëè ñåðâèñà íå å âå÷å ñïðÿí.
if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
if (ssp.dwCurrentState == SERVICE_STOPPED) {
MessageBox(hWnd, "Ñåðâèñúò å áèë ñïðåí", "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Àêî ñòîïèðàíåòî å â î÷àêâàíå, èç÷àêàé ãî.
while (ssp.dwCurrentState == SERVICE_STOP_PENDING) {
dwWaitTime = ssp.dwWaitHint / 10;
if(dwWaitTime < 1000)
dwWaitTime = 1000;
else if (dwWaitTime > 10000)
dwWaitTime = 10000;
Sleep(dwWaitTime);
if ( !QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
if (ssp.dwCurrentState == SERVICE_STOPPED) {
MessageBox(hWnd, "Ñåðâèñúò å ñïðÿí", "FileHide", MB_ICONEXCLAMATION);
CloseServiceHandle(schService);
return TRUE;
}
if (GetTickCount() - dwStartTime > dwTimeout) {
MessageBox(hWnd, "Âðåìåòî çà ñïèðàíå íà ñåðâèñà èçòå÷å", "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
}
// Àêî ñåðâèñà ðàáîòè, çàâèñèìèòå ñåðâèñè ïúðâî ñå ñïèðàò.
StopDependentServices(schSCManager, schService);
// Èçïðàùà ñòîï êîä êúì ñåðâèñà.
if (!ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp)) {
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Èç÷àêà ñåðâèñà äà ñïðå.
while (ssp.dwCurrentState != SERVICE_STOPPED) {
Sleep(ssp.dwWaitHint);
if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
if (ssp.dwCurrentState == SERVICE_STOPPED)
break;
if (GetTickCount() - dwStartTime > dwTimeout) {
MessageBox(hWnd, "Âðåìåòî çà ñïèðàíå íà ñåðâèñà èçòå÷å", "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
}
MessageBox(hWnd, "Ñåðâèñúò å ñïðÿí", "FileHide", MB_ICONEXCLAMATION);
CloseServiceHandle(schService);
return TRUE;
}
BOOL StopDependentServices(SC_HANDLE hSCManager, SC_HANDLE hService)
{
LPENUM_SERVICE_STATUS lpDependencies = NULL;
ENUM_SERVICE_STATUS ess;
SC_HANDLE hDepService;
SERVICE_STATUS_PROCESS ssp;
DWORD i, dwBytesNeeded, dwCount;
DWORD dwStartTime = GetTickCount();
DWORD dwTimeout = 30000; // 30 ñåêóíäíà ïàóçà
// Ïóñêàíå íà áóôåð ñ íóëåâà äúëæèíà, çà äà ñå ïîëó÷è æåëàíèÿ ðàçìåð íà áóôåðà.
if (EnumDependentServices(hService, SERVICE_ACTIVE, lpDependencies, 0, &dwBytesNeeded, &dwCount))
// Àêî èçâèêâàíåòî íà Enum-à å óñïåøíî, òîãàâà íÿìà çàâèñèìè
// ñåðâèñè, òàêà ÷å íå ñå ïðàâè íèùî.
return TRUE;
if (GetLastError() != ERROR_MORE_DATA)
return FALSE; // Íåî÷àêâàíà ãðåøêà
// Àëîêèðàíå íà áóôåð çà çàâèñèìîñòèòå.
lpDependencies = (LPENUM_SERVICE_STATUS)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytesNeeded);
if (!lpDependencies)
return FALSE;
__try {
// Èçáðîéâàò ñå çàâèñèìîñòèòå.
if (!EnumDependentServices(hService, SERVICE_ACTIVE, lpDependencies,
dwBytesNeeded, &dwBytesNeeded, &dwCount))
return FALSE;
for (i=0; i<dwCount; i++) {
ess = *(lpDependencies + i);
// Îòâàðÿ ñå ñåðâèñà
hDepService = OpenService(hSCManager, ess.lpServiceName, SERVICE_STOP|SERVICE_QUERY_STATUS);
if (!hDepService)
return FALSE;
__try {
// Èçïðàùà ñå ñòîï êîä.
if (!ControlService(hDepService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp))
return FALSE;
// Èç÷àêâà ñå ñåðâèñà äà ñïðå.
while (ssp.dwCurrentState != SERVICE_STOPPED) {
Sleep(ssp.dwWaitHint);
if (!QueryServiceStatusEx(hDepService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp,
sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
return FALSE;
if (ssp.dwCurrentState == SERVICE_STOPPED)
break;
if (GetTickCount() - dwStartTime > dwTimeout)
return FALSE;
}
} __finally {
// Âèíàãè ñå èçïóñêà äðúæêàòà íà ñåðâèñà.
CloseServiceHandle(hDepService);
}
}
} __finally {
// Âèíàãè ñå îñâîáîæäàâà èçáðîåíèÿ áóôåð.
HeapFree(GetProcessHeap(), 0, lpDependencies);
}
return TRUE;
}
BOOL IsServiceInstalled(SC_HANDLE schSCManager, char *szSvcName)
{
SC_HANDLE schService;
schService = OpenService(
schSCManager, // SCM áàçà ñ äàííè
szSvcName, // èìå íà ñåðâèñà
SERVICE_ALL_ACCESS); // ïúëåí äîñòúï
if (schService != NULL)
return TRUE;
return FALSE;
}
BOOL IsServiceRunning(HWND hWnd, SC_HANDLE schSCManager, char *szSvcName)
{
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwBytesNeeded;
schService = OpenService(
schSCManager, // SCM áàçà ñ äàííè
szSvcName, // èìå íà ñåðâèñà
SERVICE_ALL_ACCESS); // ïúëåí äîñòúï
if (schService == NULL) {
return FALSE;
}
if (!QueryServiceStatusEx(
schService, // äðúæêà äî ñåðâèñà
SC_STATUS_PROCESS_INFO, // èíôîðìàöèîííî íèâî
(LPBYTE) &ssStatus, // àäðåñà íà ñòðóêòóðà
sizeof(SERVICE_STATUS_PROCESS), // ãîëåìèíà íà ñòðóêòóðà
&dwBytesNeeded)) // íåîáõîäèìà ãîëåìèíà, àêî áóôåðà å ìíîãî ìàëúê
{
MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR);
CloseServiceHandle(schService);
return FALSE;
}
// Ïðîâåðÿâà äàëè ñåðâèñà ðàáîòè.
if(ssStatus.dwCurrentState == SERVICE_RUNNING) {
CloseServiceHandle(schService);
return TRUE;
}
return FALSE;
}