Hier der Code um einen Service zu starten:
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:
| function ServiceStart(aMachine, aServiceName: String): Boolean; var h_manager, h_svc: SC_Handle; ServiceStatus: TServiceStatus; dwCheckPoint: DWORD; ServiceArgVectors : PChar ; begin h_manager := OpenSCManager(PChar(aMachine), nil,SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager, PChar(aServiceName), SERVICE_START or SERVICE_QUERY_STATUS); if h_svc > 0 then begin if (StartService(h_svc, 0, ServiceArgVectors)) then begin if (QueryServiceStatus(h_svc, ServiceStatus))then begin while (SERVICE_RUNNING <> ServiceStatus.dwCurrentState)do begin dwCheckPoint := ServiceStatus.dwCheckPoint; Sleep(ServiceStatus.dwWaitHint); if (not QueryServiceStatus(h_svc,ServiceStatus)) then break; if (ServiceStatus.dwCheckPoint < dwCheckPoint)then break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end;
Result := (SERVICE_RUNNING = ServiceStatus.dwCurrentState); end; |
Um einen Service zu stoppen ist folgender Code anzuwenden:
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:
| function ServiceStop(aMachine, aServiceName:String): Boolean; var h_manager, h_svc: SC_Handle; ServiceStatus: TServiceStatus; dwCheckPoint: DWORD; begin h_manager := OpenSCManager(PChar(aMachine), nil,SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager, PChar(aServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS); if h_svc > 0 then begin if (ControlService(h_svc, SERVICE_CONTROL_STOP,ServiceStatus)) then begin if (QueryServiceStatus(h_svc, ServiceStatus))then begin while (SERVICE_STOPPED <> ServiceStatus.dwCurrentState)do begin dwCheckPoint := ServiceStatus.dwCheckPoint; Sleep(ServiceStatus.dwWaitHint); if (not QueryServiceStatus(h_svc,ServiceStatus)) then break; if (ServiceStatus.dwCheckPoint < dwCheckPoint)then break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end;
Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState); end; |
Nicht vergessen die Unit WinSvc einzubinden...
Verfasst von Tony
Bis dann
_________________
Mit dem Computer löst man Probleme, die man ohne ihn nicht hätte.
Entwickler von SpaceTrek: The New Empire -
Siehe Hompage!