Uses
WinSvc;
Function vtServiceStart(Const p_strComp,p_strServiceName:String):Boolean;
Var
hManager,hSvc:SC_Handle;
nSvcStatus:TServiceStatus;
Temp:PChar;
dwCheckPoint:DWord;
Begin
ZeroMemory(@nSvcStatus,SizeOf(nSvcStatus));
nSvcStatus.dwCurrentState:=1;
hManager:=OpenSCManager(PChar(p_strComp),Nil,SC_MANAGER_CONNECT);
If hManager>0 Then
Begin
hSvc:=OpenService(hManager,PChar(p_strServiceName),SERVICE_START or SERVICE_QUERY_STATUS);
If hSvc>0 Then
Begin
Temp:=nil;
If StartService(hSvc,0,temp) Then
If QueryServiceStatus(hSvc,nSvcStatus) Then
While SERVICE_RUNNING<>nSvcStatus.dwCurrentState Do
Begin
dwCheckPoint:=nSvcStatus.dwCheckPoint;
Sleep(nSvcStatus.dwWaitHint);
If Not QueryServiceStatus(hSvc,nSvcStatus) Then Break;
If (nSvcStatus.dwCheckPoint<dwCheckPoint) then Break;
End;
CloseServiceHandle(hSvc);
End;
CloseServiceHandle(hManager);
End;
Result:=SERVICE_RUNNING=nSvcStatus.dwCurrentState;
End;
Function vtServiceStop(Const p_strMachine,p_strServiceName:String):Boolean;
Var
hManager,hSvc:SC_Handle;
nSvcStatus:TServiceStatus;
dwCheckPoint:DWord;
Begin
hManager:=OpenSCManager(PChar(p_strMachine),Nil,SC_MANAGER_CONNECT);
If hManager>0 Then
Begin
hSvc:=OpenService(hManager,PChar(p_strServiceName),SERVICE_STOP or SERVICE_QUERY_STATUS);
If hSvc>0 Then
Begin
If ControlService(hSvc,SERVICE_CONTROL_STOP,nSvcStatus) Then
If QueryServiceStatus(hSvc,nSvcStatus) Then
While SERVICE_STOPPED<>nSvcStatus.dwCurrentState Do
Begin
dwCheckPoint:=nSvcStatus.dwCheckPoint;
Sleep(nSvcStatus.dwWaitHint);
If Not QueryServiceStatus(hSvc,nSvcStatus) Then Break;
If nSvcStatus.dwCheckPoint<dwCheckPoint Then Break;
End;
CloseServiceHandle(hSvc);
End;
CloseServiceHandle(hManager);
End;
Result:=SERVICE_STOPPED=nSvcStatus.dwCurrentState;
End;
Function vtServiceGetStatus(Const p_strMachine,p_strService:String):DWord;
Var
hManager,hSvc:SC_Handle;
nSvcStatus:TServiceStatus;
hStat:DWord;
Begin
hStat:=1;
hManager:=OpenSCManager(PChar(p_strMachine),Nil,SC_MANAGER_CONNECT);
If hManager>0 Then
Begin
hSvc:=OpenService(hManager,PChar(p_strService),SERVICE_QUERY_STATUS);
If hSvc>0 Then
Begin
If QueryServiceStatus(hSvc,nSvcStatus) Then hStat:=nSvcStatus.dwCurrentState;
CloseServiceHandle(hSvc);
End;
CloseServiceHandle(hManager);
End;
Result:=hStat;
End;