Author: Tomas Rutkauskas
Want to get a list of active, inactive or all Windows services?
Answer:
Following function can help you to do this, but be sure to read other Windows
services related tips for details.
1 const
2 //
3 // Service Types
4 //
5 SERVICE_KERNEL_DRIVER = $00000001;
6 SERVICE_FILE_SYSTEM_DRIVER = $00000002;
7 SERVICE_ADAPTER = $00000004;
8 SERVICE_RECOGNIZER_DRIVER = $00000008;
9
10 SERVICE_DRIVER =
11 (SERVICE_KERNEL_DRIVER or
12 SERVICE_FILE_SYSTEM_DRIVER or
13 SERVICE_RECOGNIZER_DRIVER);
14
15 SERVICE_WIN32_OWN_PROCESS = $00000010;
16 SERVICE_WIN32_SHARE_PROCESS = $00000020;
17 SERVICE_WIN32 =
18 (SERVICE_WIN32_OWN_PROCESS or
19 SERVICE_WIN32_SHARE_PROCESS);
20
21 SERVICE_INTERACTIVE_PROCESS = $00000100;
22
23 SERVICE_TYPE_ALL =
24 (SERVICE_WIN32 or
25 SERVICE_ADAPTER or
26 SERVICE_DRIVER or
27 SERVICE_INTERACTIVE_PROCESS);
28
29 uses WinSvc;
30
31 //-------------------------------------
32 // Get a list of services
33 //
34 // return TRUE if successful
35 //
36 // sMachine:
37 // machine name, ie: \\SERVER
38 // empty = local machine
39 //
40 // dwServiceType
41 // SERVICE_WIN32,
42 // SERVICE_DRIVER or
43 // SERVICE_TYPE_ALL
44 //
45 // dwServiceState
46 // SERVICE_ACTIVE,
47 // SERVICE_INACTIVE or
48 // SERVICE_STATE_ALL
49 //
50 // slServicesList
51 // TStrings variable to storage
52 //
53
54 function ServiceGetList(
55 sMachine: string;
56 dwServiceType,
57 dwServiceState: DWord;
58 slServicesList: TStrings)
59 : boolean;
60 const
61 //
62 // assume that the total number of
63 // services is less than 4096.
64 // increase if necessary
65 cnMaxServices = 4096;
66
67 type
68 TSvcA = array[0..cnMaxServices]
69 of TEnumServiceStatus;
70 PSvcA = ^TSvcA;
71
72 var
73 //
74 // temp. use
75 j: integer;
76
77 //
78 // service control
79 // manager handle
80 schm: SC_Handle;
81
82 //
83 // bytes needed for the
84 // next buffer, if any
85 nBytesNeeded,
86
87 //
88 // number of services
89 nServices,
90
91 //
92 // pointer to the
93 // next unread service entry
94 nResumeHandle: DWord;
95
96 //
97 // service status array
98 ssa: PSvcA;
99 begin
100 Result := false;
101
102 // connect to the service
103 // control manager
104 schm := OpenSCManager(
105 PChar(sMachine),
106 nil,
107 SC_MANAGER_ALL_ACCESS);
108
109 // if successful...
110 if (schm > 0) then
111 begin
112 nResumeHandle := 0;
113
114 New(ssa);
115
116 EnumServicesStatus(
117 schm,
118 dwServiceType,
119 dwServiceState,
120 ssa^[0],
121 SizeOf(ssa^),
122 nBytesNeeded,
123 nServices,
124 nResumeHandle);
125
126 //
127 // assume that our initial array
128 // was large enough to hold all
129 // entries. add code to enumerate
130 // if necessary.
131 //
132
133 for j := 0 to nServices - 1 do
134 begin
135 slServicesList.
136 Add(StrPas(
137 ssa^[j].lpDisplayName));
138 end;
139
140 Result := true;
141
142 Dispose(ssa);
143
144 // close service control
145 // manager handle
146 CloseServiceHandle(schm);
147 end;
148 end;
149
150 //To get a list of all Windows services into a listbox named ListBox1:
151
152 ServiceGetList('', SERVICE_WIN32, SERVICE_STATE_ALL, ListBox1.Items);
|