Einzelnen Beitrag anzeigen

Astat

Registriert seit: 2. Dez 2009
Ort: München
320 Beiträge
 
Lazarus
 
#2

Re: Windowsdienst mit Delphi starten

  Alt 11. Mai 2010, 14:03
Zitat von hirsch:
..an das Thema am besten herangehen?
Delphi-Quellcode:

unit utils;

interface

uses
  windows, SysUtils, classes, WinSvc;

  function ServiceGetStatus(szMachine, szService: PChar): DWORD;
  
  function ServiceRunning(szMachine, szService: PChar): Boolean;

  function ServiceStart(szMachine, szServiceName: PChar;
    ServiceArgVectors: array of PChar): Boolean;

  function ServiceStop(szMachine, szServiceName: PChar;
    Timeout: DWORD = 60000): Boolean;

  function ServiceRegister(szMachine, szServiceFile, szServiceName,
    szDisplayName: PChar): Boolean;

  function ServiceUnRegister(szMachine, szServiceName: PChar): Boolean;

implementation


{*------------------------------------------------------------------------------
  Function zur Ermittlung eines Service Status
  @param szMachine PChar Name des Zielcomputers
  @param szService PChar Name des Services
  @return -1 = ERROR
  @return  1 = SERVICE_STOPPED
  @return  2 = SERVICE_START_PENDING
  @return  3 = SERVICE_STOP_PENDING
  @return  4 = SERVICE_RUNNING
  @return  5 = SERVICE_CONTINUE_PENDING
  @return  6 = SERVICE_PAUSE_PENDING
  @return  7 = SERVICE_PAUSED
-------------------------------------------------------------------------------}

function ServiceGetStatus(szMachine, szService: PChar): DWORD;
var
  SCManHandle, SvcHandle: SC_Handle;
  SS: TServiceStatus;
  dwStat: DWORD;
begin
  dwStat := 0;
  SCManHandle := OpenSCManager(szMachine, nil, SC_MANAGER_CONNECT);
  if (SCManHandle > 0) then begin
    SvcHandle := OpenService(SCManHandle, szService, SERVICE_QUERY_STATUS);
    if (SvcHandle > 0) then begin
      if (QueryServiceStatus(SvcHandle, SS)) then
        dwStat := ss.dwCurrentState;
      CloseServiceHandle(SvcHandle);
    end;
    CloseServiceHandle(SCManHandle);
  end;
  Result := dwStat;
end;

function ServiceRunning(szMachine, szService: PChar): Boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(szMachine, szService);
end;


// szMachine ist UNC path oder nil wenn local machine
function ServiceStart(szMachine, szServiceName: PChar;
  ServiceArgVectors: array of PChar): Boolean;
var
  h_manager, h_svc: SC_Handle;
  ServiceStatus: TServiceStatus;
  dwCheckPoint: DWORD;
  szServiceArgVectors: PPChar;
begin
  h_manager := OpenSCManager(szMachine, nil, SC_MANAGER_CONNECT);
  if h_manager > 0 then begin
    h_svc := OpenService(h_manager, szServiceName,
      SERVICE_START or SERVICE_QUERY_STATUS or SC_MANAGER_ALL_ACCESS);
    if h_svc > 0 then begin
      szServiceArgVectors := nil;
      if Length(ServiceArgVectors) > 0 then
        szServiceArgVectors := @ServiceArgVectors[0];

      if (StartService(h_svc, Length(ServiceArgVectors), szServiceArgVectors^)) 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;

// szMachine ist UNC path oder nil wenn local machine
function ServiceStop(szMachine, szServiceName: PChar;
  Timeout: DWORD = 60000): Boolean;
var
  h_manager, h_svc: SC_Handle;
  ServiceStatus: TServiceStatus;
  dwCheckPoint: DWORD;
  dwStart: DWORD;
begin
  h_manager := OpenSCManager(szMachine, nil, SC_MANAGER_CONNECT);
  if h_manager > 0 then begin
    h_svc := OpenService(h_manager, szServiceName,
      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
          dwStart := GetTickCount;
          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;
            if GetTickCount - dwStart > Timeout then break;
          end;
        end;
      end;
      CloseServiceHandle(h_svc);
    end;
    CloseServiceHandle(h_manager);
  end;
  Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState);
end;

function ServiceRegister(szMachine, szServiceFile, szServiceName,
  szDisplayName: PChar): Boolean;
var
  h_manager, h_svc: SC_Handle;
begin
  Result := false;

  h_manager := OpenSCManager(szMachine, nil, SC_MANAGER_ALL_ACCESS);
  if h_manager > 0 then begin

    h_svc := CreateService(h_manager,
      szServiceName,
      szDisplayName,
      SERVICE_START or SERVICE_QUERY_STATUS or _DELETE,
      SERVICE_WIN32_OWN_PROCESS, // or SERVICE_INTERACTIVE_PROCESS,
      SERVICE_AUTO_START,
      SERVICE_ERROR_NORMAL,
      szServiceFile,
      nil, nil, nil, nil, nil);

    if h_svc > 0 then begin
      result := true;
      CloseServiceHandle(h_svc);
    end;

    CloseServiceHandle(h_manager);
  end;
end;

function ServiceUnRegister(szMachine, szServiceName: PChar): Boolean;
var
  h_manager, h_svc: SC_Handle;
begin
  Result := false;

  h_manager := OpenSCManager(szMachine, nil, SC_MANAGER_ALL_ACCESS);
  if h_manager > 0 then begin

    h_svc := OpenService(h_manager, szServiceName, SERVICE_ALL_ACCESS);
    if h_svc > 0 then begin
      result := DeleteService(h_svc);
      CloseServiceHandle(h_svc);
    end;

    CloseServiceHandle(h_manager);
  end;
end;

end.
lg. Astat
Lanthan Astat
06810110811210410503210511511603209711003210010110 9032084097103
03211611111604403209711003210010110903210010510103 2108101116122
11610103209010110510810103206711110010103210511003 2068101108112
10410503210310111509910411410510109810111003211910 5114100046
  Mit Zitat antworten Zitat