Delphi 系统服务是否存在,启动及信息
1 //-------------------------------------
2 // 获取某个系统服务的当前状态
3 //
4 // return status code if successful
5 // -1 if not
6 //
7 // return codes:
8 // SERVICE_STOPPED
9 // SERVICE_RUNNING
10 // SERVICE_PAUSED
11 //
12 // following return codes are used to indicate that the service is in the
13 // middle of getting to one of the above states:
14 // SERVICE_START_PENDING
15 // SERVICE_STOP_PENDING
16 // SERVICE_CONTINUE_PENDING
17 // SERVICE_PAUSE_PENDING
18 //
19 // sMachine:
20 // machine name, ie: \SERVER
21 // empty = local machine
22 //
23 //sService
24 // service name, ie: Alerter
25 //
26 function TFormConfig.ServiceGetStatus(sMachine, sService: string ): DWord;
27 var
28 //service control
29 //manager handle
30 schm,
31 //service handle
32 schs: SC_Handle;
33 //service status
34 ss: TServiceStatus;
35 //current service status
36 dwStat : DWord;
37 begin
38 dwStat := 0;
39 //connect to the service
40 //control manager
41 schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
42 //if successful...
43 if(schm > 0)then
44 begin
45 //open a handle to
46 //the specified service
47 schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);
48 //if successful...
49 if(schs > 0)then
50 begin
51 //retrieve the current status
52 //of the specified service
53 if(QueryServiceStatus(schs, ss))then
54 begin
55 dwStat := ss.dwCurrentState;
56 end;
57 //close service handle
58 CloseServiceHandle(schs);
59 end;
60
61 // close service control
62 // manager handle
63 CloseServiceHandle(schm);
64 end;
65
66 Result := dwStat;
67 end;
68
69 {判断某服务是否安装,未安装返回true,已安装返回false}
70 function TFormConfig.ServiceUninstalled(sMachine, sService : string ) : boolean;
71 begin
72 Result := 0 = ServiceGetStatus(sMachine, sService);
73 end;
74
75 {判断某服务是否启动,启动返回true,未启动返回false}
76 function TFormConfig.ServiceRunning(sMachine, sService : string ) : boolean;
77 begin
78 Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService );
79 end;
80
81 {判断某服务是否停止,停止返回true,未停止返回false}
82 function TFormConfig.ServiceStopped(sMachine, sService : string ) : boolean;
83 begin
84 Result := SERVICE_STOPPED = ServiceGetStatus(sMachine, sService );
85 end;