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 ;

 

转载于:https://www.cnblogs.com/RoyGo/archive/2009/09/15/1906994.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值