Delphi determine whether there is a system service and related state

记得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 
the begin  
dwStat: = ss.dwCurrentState; 
 End ; 
 // Close-Service handle 
CloseServiceHandle (SCHS); 
 End ; 

// Close-Service Control 
// Manager handle 
CloseServiceHandle (SCHM); 
 End ; 

the Result: = dwStat; 
 End ; 

{ determines whether a service installation, the installation does not return true, installed returns to false }  
function TFormConfig.ServiceUninstalled (sMachine, Sservice: String ): Boolean; 
 the begin  
the Result: = 0 = ServiceGetStatus (sMachine, Sservice); 
 End; 

{ Determining whether to activate a service, start returns true, does not start to return to false }  
function TFormConfig.ServiceRunning (sMachine, Sservice: String ): Boolean; 
 the begin  
the Result: = SERVICE_RUNNING = ServiceGetStatus (sMachine, Sservice); 
 End ; 

{ Analyzing a service is stopped, stop returns true, does not stop returns to false }  
function TFormConfig.ServiceStopped (sMachine, Sservice: String ): Boolean; 
 the begin  
the Result: = = SERVICE_STOPPED ServiceGetStatus (sMachine, Sservice); 
 End ;

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11345364.html