-
Notifications
You must be signed in to change notification settings - Fork 14
/
Helpers.Services.pas
45 lines (37 loc) · 1.26 KB
/
Helpers.Services.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
unit Helpers.Services;
interface
uses
Winapi.Windows, Winapi.WinSvc;
function ServiceGetStatus(sMachine, sService: LPCTSTR): DWORD;
function IsServiceRunning(sMachine, sService: LPCTSTR): Boolean;
function IsServiceExisting(sMachine, sService: LPCTSTR): Boolean;
implementation
function ServiceGetStatus(sMachine, sService: LPCTSTR): DWORD;
var
SCManager, Service: SC_Handle;
ServiceStatus: TServiceStatus;
begin
Result := 0;
// Open service manager handle.
SCManager := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
if SCManager > 0 then begin
Service := OpenService(SCManager, sService, SERVICE_QUERY_STATUS);
// if Service installed
if (Service > 0) then begin
// SS structure holds the service status (TServiceStatus);
if (QueryServiceStatus(Service, ServiceStatus)) then
Result := ServiceStatus.dwCurrentState;
CloseServiceHandle(Service);
end;
CloseServiceHandle(SCManager);
end;
end;
function IsServiceRunning(sMachine, sService: LPCTSTR): Boolean;
begin
Result := ServiceGetStatus(sMachine, sService) = SERVICE_RUNNING;
end;
function IsServiceExisting(sMachine, sService: LPCTSTR): Boolean;
begin
Result := ServiceGetStatus(sMachine, sService) <> 0;
end;
end.