Autor Beitrag
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Do 24.07.03 08:59 
Ich suche nach einer Möglichkeit die laufenden Dienste in einer ListBox anzuzeigen. Mein Problem besteht darin, dass ich nur einzelne Dienste mit Hilfe ihres Namens auf ihren Status überprüfen kann und nicht alle.

1. Gibt es eine Möglichkeit die Namen aller Dienste zu bekommen?

oder

2. Gibt es eine Funktion, die mir alle laufenden Dienste liefert?

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 24.07.03 13:06 
Alle Dienste auflisten und deren Status inein Memo eintragen:
ausblenden volle Höhe Delphi-Quelltext
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:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
uses
  WinSvc;

var
  hLib : Cardinal;

type
  PENUM_SERVICE_STATUS = ^ENUM_SERVICE_STATUS;
  ENUM_SERVICE_STATUS =
    packed record
      lpServiceName : PChar;
      lpDisplayName : PChar;
      ServiceStatus : SERVICE_STATUS;
    end;

type
  TcsEnumServicesStatus = function(
    const hSCManager         : DWord;                // handle to SCM database
    const dwServiceType      : DWord;                // service type
    const dwServiceState     : DWord;                // service state
    const lpServices         : PENUM_SERVICE_STATUS; // status buffer
    const cbBufSize          : DWord;                // size of status buffer
    const pcbBytesNeeded     : PDWORD;               // buffer size needed
    const lpServicesReturned : PDWord;               // number of entries returned
    const lpResumeHandle     : PDWord                // next entry
    ) : Boolean; stdcall;


  TcsOpenSCManager = function(
    const lpMachineName   : PChar;
    const lpDatabaseName  : PChar;
    const dwDesiredAccess : DWord
    ) : DWord; stdcall;


var
  EnumServicesStatus    : TcsEnumServicesStatus;
  OpenSCManager         : TcsOpenSCManager;

procedure TForm1.Button1Click(Sender: TObject);
var
  hSC: Cardinal;
  pStatus            : PENUM_SERVICE_STATUS;
  pWork              : PENUM_SERVICE_STATUS;
  cbBufSize          : DWord; 
  pcbBytesNeeded     : DWord; 
  lpServicesReturned : DWord; 
  lpResumeHandle     : DWord; 
  i                  : integer;
  s                  : String;
  ss                 : SERVICE_STATUS;
begin
  hSC := OpenSCManager(nilnil, SC_MANAGER_ENUMERATE_SERVICE);
  if hSC <> 0 then
  begin
    try
      cbBufSize      := 0;
      pStatus        := nil;
      lpResumeHandle := 0;
      EnumServicesStatus(hSC,SERVICE_WIN32,SERVICE_STATE_ALL,pStatus,
        cbBufSize,@pcbBytesNeeded,@lpServicesReturned,@lpResumeHandle);
      pStatus := AllocMem(pcbBytesNeeded);
      try
        cbBufSize := pcbBytesNeeded;
        EnumServicesStatus(hSC,SERVICE_WIN32,SERVICE_STATE_ALL,pStatus,
          cbBufSize,@pcbBytesNeeded,@lpServicesReturned,@lpResumeHandle);
        pWork := pStatus;
        for i := 1 to lpServicesReturned do
        begin
          s := pWork.lpDisplayName;
          case pWork.ServiceStatus.dwCurrentState of
            SERVICE_CONTINUE_PENDING : s := s+' (wird fortgesetzt';
            SERVICE_PAUSED : s := s+' (pausiert';
            SERVICE_RUNNING: s := s+' (gestartet';
            SERVICE_START_PENDING: s := s+' (wird gestartet';
            SERVICE_STOP_PENDING: s := s+' (wird gestoppt';
            SERVICE_STOPPED: s := s+' (gestoppt';
          end;
          case pWork.ServiceStatus.dwServiceType of
            SERVICE_FILE_SYSTEM_DRIVER : s := s+', Dateitreiber)';
            SERVICE_KERNEL_DRIVER      : s := s+', Gerätetreiber)';
            SERVICE_WIN32_OWN_PROCESS  : s := s+', eigener Prozess)';
            SERVICE_WIN32_SHARE_PROCESS: s := s+', teilt Prozess)'
          else
            s := s+')';
          end;
          Memo1.Lines.Add(s);
          inc(pWork);
        end;
      finally
        if Assigned(pStatus) then
        begin
          FreeMem(pStatus,pcbBytesNeeded);
        end;
      end;
    finally
      CloseServiceHandle(hSC);
    end;
  end
  else
    RaiselastOSError();
end;

initialization
begin
  hLib := LoadLibrary('ADVAPI32.DLL');
  if hLib <> 0 then
  begin
    @EnumServicesStatus := GetProcAddress(hLib,'EnumServicesStatusA');
    if @EnumServicesStatus = nil then raise Exception.Create('EnumServicesStatusA');
    @OpenSCManager := GetProcAddress(hLib,'OpenSCManagerA');
    if @OpenSCManager = nil then raise Exception.Create('OpenSCManagerA');
  end;
end;

finalization
begin
  if hLib <> 0 then
  begin
    FreeLibrary(hLib);
  end;
end;