Einzelnen Beitrag anzeigen

Benutzerbild von Jelly
Jelly

Registriert seit: 11. Apr 2003
Ort: Moestroff (Luxemburg)
3.741 Beiträge
 
Delphi 2007 Professional
 
#2
  Alt 11. Apr 2003, 23:32
Hi,

um Services zu starten/stoppen musst du nicht unbedingt Admin sein, sondern du brauchst einfach die Rechte, Services zu starten und zu stoppen. Hast du die nicht, haste de keine Chance. Aber hier mal bischen Code zum Starten und Stoppen eines NT Services:

Start:
Code:
function ServiceStart(aMachine, aServiceName: String): Boolean;
// aMachine 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(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 { 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;
und zum Stoppen:
Code:
function ServiceStop(aMachine, aServiceName:String): Boolean;
// aMachine is UNC path or local machine if empty
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
              // 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;
Gruß,
Tom
Tom Peiffer
  Mit Zitat antworten Zitat