|
Registriert seit: 30. Mai 2007 489 Beiträge Delphi 2010 Professional |
#7
Und die Lösungen funktionieren?
Der vollständigkeit halber poste ich's mal:
Delphi-Quellcode:
und hier noch eine Unit die ich grad noch zugeschickt gekriegt hab:
unit ServiceUtils;
// ################################### // ###### Unit for working with services // ################################### // Discussion: // http://www.delphipraxis.net/7547-pruefen-ob-service-laeuft.html#post1122577 interface function ServiceGetStatus(sMachine, sService: PChar): Integer; function IsServiceRunning(sMachine, sService: PChar): Boolean; function IsServiceInstalled(sMachine, sService: PChar): Boolean; function ServiceStart(Machine, ServiceName: string): Boolean; function ServiceStop(Machine, ServiceName: string): Boolean; implementation uses WinSvc, Windows; // ################################### // ###### // ################################### // This block taken from // http://www.delphipraxis.net/101017-wie-pruefen-ob-service-dienst-installiert-nicht-laeuft.html function ServiceGetStatus(sMachine, sService: PChar): Integer; {******************************************} {*** Parameters: ***} {*** sService: specifies the name of the service to open {*** sMachine: specifies the name of the target computer {*** ***} {*** Return Values: ***} {*** -1 = Error opening service ***} {*** 1 = SERVICE_STOPPED ***} {*** 2 = SERVICE_START_PENDING ***} {*** 3 = SERVICE_STOP_PENDING ***} {*** 4 = SERVICE_RUNNING ***} {*** 5 = SERVICE_CONTINUE_PENDING ***} {*** 6 = SERVICE_PAUSE_PENDING ***} {*** 7 = SERVICE_PAUSED ***} {******************************************} var SCManHandle, SvcHandle: SC_Handle; SS: TServiceStatus; dwStat: Integer; begin dwStat := -1; // Open service manager handle. SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT); if (SCManHandle > 0) then begin SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS); // if Service installed if (SvcHandle > 0) then begin // SS structure holds the service status (TServiceStatus); if (QueryServiceStatus(SvcHandle, SS)) then dwStat := ss.dwCurrentState; CloseServiceHandle(SvcHandle); end; CloseServiceHandle(SCManHandle); end; Result := dwStat; end; function IsServiceRunning(sMachine, sService: PChar): Boolean; begin Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService); end; function IsServiceInstalled(sMachine, sService: PChar): Boolean; begin Result := -1 <> ServiceGetStatus(sMachine, sService); end; // ################################### // ###### // ################################### // This block taken from here: // http://www.delphipraxis.net/88581-dienst-service-starten-stoppen-oder-status-abfragen.html function ServiceStart(Machine, ServiceName: string): Boolean; // Machine is UNC path or local machine if empty var h_manager, h_svc: SC_Handle; ServiceStatus: TServiceStatus; dwCheckPoint: DWORD; ServiceArgVectors: PChar; begin h_manager := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager, PChar(ServiceName), SERVICE_START or SERVICE_QUERY_STATUS or SC_MANAGER_ALL_ACCESS); if h_svc > 0 then begin if (StartService(h_svc, 0, ServiceArgVectors)) then { succeeded } 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 // couldn't check status break; if (ServiceStatus.dwCheckPoint < dwCheckPoint) then break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := (SERVICE_RUNNING = ServiceStatus.dwCurrentState); end; function ServiceStop(Machine, ServiceName: string): Boolean; // Machine is UNC path or local machine if empty var h_manager, h_svc: SC_Handle; ServiceStatus: TServiceStatus; dwCheckPoint: DWORD; begin h_manager := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager, PChar(ServiceName), 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 // couldn't check status break; if (ServiceStatus.dwCheckPoint < dwCheckPoint) then break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState); end; end.
Delphi-Quellcode:
{*******************************************************************************
Description: provides functions to control and check status of service applications Last change: 26. July 2009 based on http://www.delphipraxis.net/topic105934_dienstservice+starten+stoppen+oder+status+abfragen.html *******************************************************************************} unit ServiceFunctions; interface {$IFNDEF UNICODE} type UnicodeString = WideString; {$ENDIF} function IsServiceInstalled(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; function IsServiceRunning(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; // the following functions need admin rights (will trigger UAC under Vista+) function ServiceStart(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; function ServiceStop(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; function ServiceRestart(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; implementation uses Windows, WinSvc{$IFNDEF UNICODE}, SysUtils{$ENDIF}; //------------------------------------------------------------------------------ // internal helper functions //------------------------------------------------------------------------------ function WrappedOpenServiceManager(const MachineName: UnicodeString): SC_HANDLE; begin {$IFNDEF UNICODE} if (Win32Platform = VER_PLATFORM_WIN32_NT) then Result := OpenSCManagerW(PWideChar(MachineName), nil, SC_MANAGER_CONNECT) else {$ENDIF} // Note: the following uses the Wide-Version when using an Unicode capable // Delphi version (D2009+) but the Ansi-Version with lower Delphi versions. // Do not remove the string() type for this wanted and correct behaviour. Result := OpenSCManager(PChar(string(MachineName)), nil, SC_MANAGER_CONNECT); end; function WrappedOpenService(const ManagerHandle: SC_HANDLE; const ServiceName: UnicodeString; const DesiredAccess: Cardinal): SC_HANDLE; begin {$IFNDEF UNICODE} if (Win32Platform = VER_PLATFORM_WIN32_NT) then Result := OpenServiceW(ManagerHandle, PWideChar(ServiceName), DesiredAccess) else {$ENDIF} // Note: the following uses the Wide-Version when using an Unicode capable // Delphi version (D2009+) but the Ansi-Version with lower Delphi versions. // Do not remove the string() type for this wanted and correct behaviour. Result := OpenService(ManagerHandle, PChar(string(ServiceName)), DesiredAccess); end; procedure WrappedQueryServiceStatus(const SvcHandle: SC_HANDLE; var ServiceStatus: TServiceStatus; const StatusWaitingFor: Cardinal); var dwCheckPoint: DWORD; begin if QueryServiceStatus(SvcHandle, ServiceStatus) then begin while (ServiceStatus.dwCurrentState <> StatusWaitingFor) do begin dwCheckPoint := ServiceStatus.dwCheckPoint; Sleep(ServiceStatus.dwWaitHint); if (not QueryServiceStatus(SvcHandle, ServiceStatus)) then // couldn't check status Break; if (ServiceStatus.dwCheckPoint < dwCheckPoint) then Break; end; end; end; function GetServiceStatus(const ServiceName, MachineName: UnicodeString): Cardinal; { Parameters: ServiceName specifies the name of the service to open MachineName specifies the name of the target computer Return Values: 0 Error opening service 1 SERVICE_STOPPED 2 SERVICE_START_PENDING 3 SERVICE_STOP_PENDING 4 SERVICE_RUNNING 5 SERVICE_CONTINUE_PENDING 6 SERVICE_PAUSE_PENDING 7 SERVICE_PAUSED } var SCManHandle, SvcHandle: SC_Handle; ServiceStatus: TServiceStatus; dwStat: Cardinal; begin dwStat := 0; SCManHandle := WrappedOpenServiceManager(MachineName); if SCManHandle > 0 then begin try SvcHandle := WrappedOpenService(SCManHandle, ServiceName, SERVICE_QUERY_STATUS); if SvcHandle > 0 then begin try if QueryServiceStatus(SvcHandle, ServiceStatus) then dwStat := ServiceStatus.dwCurrentState; finally CloseServiceHandle(SvcHandle); end; end; finally CloseServiceHandle(SCManHandle); end; end; Result := dwStat; end; //------------------------------------------------------------------------------ // public functions //------------------------------------------------------------------------------ function IsServiceInstalled(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; begin Result := GetServiceStatus(ServiceName, MachineName) <> 0; end; function IsServiceRunning(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; begin Result := GetServiceStatus(ServiceName, MachineName) = SERVICE_RUNNING; end; function ServiceStart(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; var SCManHandle, SvcHandle: SC_Handle; ServiceStatus: TServiceStatus; ServiceArgVectors: PChar; begin SCManHandle := WrappedOpenServiceManager(MachineName); if SCManHandle > 0 then begin try SvcHandle := WrappedOpenService(SCManHandle, ServiceName, SERVICE_START or SERVICE_QUERY_STATUS or SC_MANAGER_ALL_ACCESS); if SvcHandle > 0 then begin try if StartService(SvcHandle, 0, ServiceArgVectors) then // succeeded WrappedQueryServiceStatus(SvcHandle, ServiceStatus, SERVICE_RUNNING); finally CloseServiceHandle(SvcHandle); end; end; finally CloseServiceHandle(SCManHandle); end; end; Result := (ServiceStatus.dwCurrentState = SERVICE_RUNNING); end; function ServiceStop(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; var SCManHandle, SvcHandle: SC_Handle; ServiceStatus: TServiceStatus; begin SCManHandle := WrappedOpenServiceManager(MachineName); if SCManHandle > 0 then begin try SvcHandle := WrappedOpenService(SCManHandle, ServiceName, SERVICE_STOP or SERVICE_QUERY_STATUS); if SvcHandle > 0 then begin try if ControlService(SvcHandle, SERVICE_CONTROL_STOP, ServiceStatus) then WrappedQueryServiceStatus(SvcHandle, ServiceStatus, SERVICE_STOPPED); finally CloseServiceHandle(SvcHandle); end; end; finally CloseServiceHandle(SCManHandle); end; end; Result := (ServiceStatus.dwCurrentState = SERVICE_STOPPED); end; function ServiceRestart(const ServiceName: UnicodeString; const MachineName: UnicodeString = ''): Boolean; begin Result := ServiceStop(ServiceName, MachineName) and ServiceStart(ServiceName, MachineName); end; end. |
![]() |
Themen-Optionen | Thema durchsuchen |
Ansicht | |
ForumregelnEs ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.
BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus. Trackbacks are an
Pingbacks are an
Refbacks are aus
|
|
Nützliche Links |
Heutige Beiträge |
Sitemap |
Suchen |
Code-Library |
Wer ist online |
Alle Foren als gelesen markieren |
LinkBack |
![]() |
![]() |