启动/停止服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了启动/停止服务相关的知识,希望对你有一定的参考价值。
function ServiceStart(sMachine,sService : string ) : boolean; var schm,schs : SC_Handle; ss : TServiceStatus; psTemp : PChar; dwChkP : DWord; begin ss.dwCurrentState := 0; schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT); if(schm > 0)then begin schs := OpenService(schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS); if(schs > 0)then begin psTemp := Nil; if(StartService(schs,0,psTemp))then begin if(QueryServiceStatus(schs,ss))then begin while(SERVICE_RUNNING <> ss.dwCurrentState)do begin dwChkP := ss.dwCheckPoint; Sleep(ss.dwWaitHint); if(not QueryServiceStatus(schs,ss))then begin break; end; if(ss.dwCheckPoint < dwChkP)then begin break; end; end; end; end; CloseServiceHandle(schs); end; CloseServiceHandle(schm); end; Result := SERVICE_RUNNING = ss.dwCurrentState; end; function ServiceStop(sMachine,sService : string ) : boolean; var schm,schs : SC_Handle; ss : TServiceStatus; dwChkP : DWord; begin schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT); if(schm > 0)then begin schs := OpenService(schm,PChar(sService),SERVICE_STOP or SERVICE_QUERY_STATUS); if(schs > 0)then begin if(ControlService(schs,SERVICE_CONTROL_STOP,ss))then begin if(QueryServiceStatus(schs,ss))then begin while(SERVICE_STOPPED <> ss.dwCurrentState)do begin dwChkP := ss.dwCheckPoint; Sleep(ss.dwWaitHint); if(not QueryServiceStatus(schs,ss))then begin break; end; if(ss.dwCheckPoint < dwChkP)then begin break; end; end; end; end; CloseServiceHandle(schs); end; CloseServiceHandle(schm); end; Result := SERVICE_STOPPED = ss.dwCurrentState; end;
以上是关于启动/停止服务的主要内容,如果未能解决你的问题,请参考以下文章