GetServiceList

Top  Previous  Next

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, WinSvc;

 

type

TServiceType=(stAll, stWIN32, stDriver);

TServiceState=(ssAll, ssActive, ssInactive);

 

 

function GetServiceList(FItems: TStringList):boolean;

const

st: array[TServiceType] of DWORD=(SERVICE_TYPE_ALL, SERVICE_WIN32, SERVICE_DRIVER);

ss: array[TServiceState] of DWORD=(SERVICE_STATE_ALL, SERVICE_ACTIVE, SERVICE_INACTIVE);

type

TESS=array[0..1023] of TEnumServiceStatus;

PESS=^TESS;

var

i: integer;

handle: SC_HANDLE;

dwBytesNeeded, dwServicesReturned, dwResumeHandle: DWORD;

ess: PESS;

begin

handle:=OpenSCManager(PChar(FMachine), nil, SC_MANAGER_ALL_ACCESS);

if handle>0 then begin

   FItems.Clear;

   dwResumeHandle:=0;

   New(ess);

   EnumServicesStatus(handle, st[FServiceType], ss[FServiceState], ess^[0], SizeOf(ess^), dwBytesNeeded, dwServicesReturned, dwResumeHandle);

   for i:=0 to dwServicesReturned-1 do begin

     FItems.Add(ess^[i].lpDisplayName);

   end;

   Dispose(ess);

   CloseServiceHandle(handle);

   FItems.Sort;

end;

end;

 

// Return        the exact full name of service

//        pString : a part of service name (pl: 'mysql') => Return: MySQL56

function GetServiceName(pString: String):String;

const

st: array[TServiceType] of DWORD=(SERVICE_TYPE_ALL, SERVICE_WIN32, SERVICE_DRIVER);

ss: array[TServiceState] of DWORD=(SERVICE_STATE_ALL, SERVICE_ACTIVE, SERVICE_INACTIVE);

type

TESS=array[0..1023] of TEnumServiceStatus;

PESS=^TESS;

var

i: integer;

handle: SC_HANDLE;

dwBytesNeeded, dwServicesReturned, dwResumeHandle: DWORD;

ess: PESS;

FMachine,s: string;

begin

Result := '';

handle:=OpenSCManager(PChar(FMachine), nil, SC_MANAGER_ALL_ACCESS);

if handle>0 then begin

   dwResumeHandle:=0;

   New(ess);

   EnumServicesStatus(handle, st[stAll], ss[ssAll], ess^[0], SizeOf(ess^), dwBytesNeeded, dwServicesReturned, dwResumeHandle);

   for i:=0 to dwServicesReturned-1 do begin

       s := UpperCase( ess^[i].lpDisplayName );

       if Pos(UpperCase(pString), s )>0 then begin

          Result := ess^[i].lpDisplayName;

       end;

   end;

   Dispose(ess);

   CloseServiceHandle(handle);

end;

end;