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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2021-08-13 PostgreSQL 数据库开发规范——命名规范 & 设计规范