1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
| uses JwaWinSvc;
function ControlService(const ServiceName:PChar;const Start:boolean):boolean; var schService,schSCManager:SC_Handle; sspStatus:_SERVICE_STATUS_PROCESS; ssStatus:_SERVICE_STATUS; dwOldCheckPoint,dwStartTickCount,dwWaitTime,dwBytesNeeded:DWord; begin Result:=False; schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); if schSCManager<>0 then try schService:=OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS); if schService=0 then Exit;
case Start of True: if not StartService(schService,0,nil) then Exit; False: if not jWaWinSvc.ControlService(schService,SERVICE_CONTROL_STOP, ssStatus) then Exit; end;
if not QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,@sspStatus, SizeOf(sspStatus),dwBytesNeeded ) then Exit;
dwStartTickCount:=GetTickCount; dwOldCheckPoint:=sspStatus.dwCheckPoint; while (sspStatus.dwCurrentState=SERVICE_START_PENDING) or (sspStatus.dwCurrentState=SERVICE_STOP_PENDING) do begin dwWaitTime:=sspStatus.dwWaitHint div 10; if dwWaitTime<1000 then dwWaitTime:=1000 else if dwWaitTime>10000 then dwWaitTime:=10000;
Sleep (dwWaitTime); if not QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,@sspStatus, SizeOf(sspStatus),dwBytesNeeded) then Exit;
if sspStatus.dwCheckPoint>dwOldCheckPoint then begin dwStartTickCount:=GetTickCount; dwOldCheckPoint:=sspStatus.dwCheckPoint; end else begin if GetTickCount-dwStartTickCount > sspStatus.dwWaitHint then Break; end; end;
CloseServiceHandle(schService); if sspStatus.dwCurrentState = SERVICE_RUNNING then Result:=True;
finally CloseServiceHandle(schSCManager); end; end;
function RestartService(const ServiceName:PChar):boolean; begin Result:=ControlService(ServiceName,False) and ControlService(ServiceName,True); end; |