其他分享
首页 > 其他分享> > Delphi 判断某个系统服务是否存在及相关状态

Delphi 判断某个系统服务是否存在及相关状态

作者:互联网

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

 

标签:begin,判断,end,service,SERVICE,Delphi,sService,某个,sMachine
来源: https://www.cnblogs.com/Thenext/p/16581982.html