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


本文整理自网络,侵删。

 记得use WinSvc; 

//------------------------------------- 
// 获取某个系统服务的当前状态 
// 
// return status code if successful 
// -1 if not 
// 
// return codes: 
// SERVICE_STOPPED 
// SERVICE_RUNNING 
// SERVICE_PAUSED 
// 
// following return codes are used to indicate that the service is in the 
// middle of getting to one of the above states: 
// SERVICE_START_PENDING 
// SERVICE_STOP_PENDING 
// SERVICE_CONTINUE_PENDING 
// SERVICE_PAUSE_PENDING 
// 
// sMachine: 
// machine name, ie: \SERVER 
// empty = local machine 
// 
//sService 
// service name, ie: Alerter 
// 
function TFormConfig.ServiceGetStatus(sMachine, sService: string ): DWord; 
var 
//service control 
//manager handle 
schm, 
//service handle 
schs: SC_Handle; 
//service status 
ss: TServiceStatus; 
//current service status 
dwStat : DWord; 
begin 
dwStat := 0; 
//connect to the service 
//control manager 
schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT); 
//if successful... 
if(schm > 0)then 
begin 
//open a handle to 
//the specified service 
schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS); 
//if successful... 
if(schs > 0)then 
begin 
//retrieve the current status 
//of the specified service 
if(QueryServiceStatus(schs, ss))then 
begin 
dwStat := ss.dwCurrentState; 
end; 
//close service handle 
CloseServiceHandle(schs); 
end; 

// close service control 
// manager handle 
CloseServiceHandle(schm); 
end; 

Result := dwStat; 
end; 

{判断某服务是否安装,未安装返回true,已安装返回false} 
function TFormConfig.ServiceUninstalled(sMachine, sService : string ) : boolean; 
begin 
Result := 0 = ServiceGetStatus(sMachine, sService); 
end; 

{判断某服务是否启动,启动返回true,未启动返回false} 
function TFormConfig.ServiceRunning(sMachine, sService : string ) : boolean; 
begin 
Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService ); 
end; 

{判断某服务是否停止,停止返回true,未停止返回false} 
function TFormConfig.ServiceStopped(sMachine, sService : string ) : boolean; 
begin 
Result := SERVICE_STOPPED = ServiceGetStatus(sMachine, sService ); 
end; 

相关阅读 >>

Delphi edit 让密码变成点

Delphi 实现卸载windows应用程序(类似360软件管家-卸载程序)

Delphi 10.3版本获取windows系统版本和cpu信息

Delphi opentextfiledialog用法

Delphi android / ios应用程序中使用tgeocoder类进行反向地理编码(从位置信息中获取地址)

Delphi win32,win64用于单/多线程计数素数的整数性能比较

adoquery 实现获取数据库所有表名

Delphi xe更改ttrayicon系统任务栏图标(无模糊)

Delphi firemonkey 图片显示拉伸不变形

Delphi 截取绝对路径的文件名

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...