- Do not send the WM_MOUSEACTIVATE message for a window that has no parent.
[reactos.git] / base / applications / mscutils / servman / query.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/query.c
5 * PURPOSE: Query service information
6 * COPYRIGHT: Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "precomp.h"
11
12
13 ENUM_SERVICE_STATUS_PROCESS*
14 GetSelectedService(PMAIN_WND_INFO Info)
15 {
16 LVITEM lvItem;
17
18 lvItem.mask = LVIF_PARAM;
19 lvItem.iItem = Info->SelectedItem;
20 SendMessage(Info->hListView,
21 LVM_GETITEM,
22 0,
23 (LPARAM)&lvItem);
24
25 /* return pointer to selected service */
26 return (ENUM_SERVICE_STATUS_PROCESS *)lvItem.lParam;
27 }
28
29
30 LPQUERY_SERVICE_CONFIG
31 GetServiceConfig(LPTSTR lpServiceName)
32 {
33 SC_HANDLE hSCManager = NULL;
34 SC_HANDLE hSc = NULL;
35 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
36 DWORD BytesNeeded = 0;
37
38 hSCManager = OpenSCManager(NULL,
39 NULL,
40 SC_MANAGER_ENUMERATE_SERVICE);
41 if (hSCManager == NULL)
42 {
43 GetError();
44 return NULL;
45 }
46
47 hSc = OpenService(hSCManager,
48 lpServiceName,
49 SERVICE_QUERY_CONFIG);
50 if (hSc == NULL)
51 {
52 GetError();
53 goto cleanup;
54 }
55
56 if (!QueryServiceConfig(hSc,
57 pServiceConfig,
58 0,
59 &BytesNeeded))
60 {
61 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
62 {
63 pServiceConfig = (LPQUERY_SERVICE_CONFIG) HeapAlloc(ProcessHeap,
64 0,
65 BytesNeeded);
66 if (pServiceConfig == NULL)
67 goto cleanup;
68
69 if (!QueryServiceConfig(hSc,
70 pServiceConfig,
71 BytesNeeded,
72 &BytesNeeded))
73 {
74 HeapFree(ProcessHeap,
75 0,
76 pServiceConfig);
77
78 pServiceConfig = NULL;
79 }
80 }
81 }
82
83 cleanup:
84 if (hSCManager != NULL)
85 CloseServiceHandle(hSCManager);
86 if (hSc != NULL)
87 CloseServiceHandle(hSc);
88
89 return pServiceConfig;
90 }
91
92
93 BOOL
94 SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig,
95 LPTSTR lpServiceName,
96 LPTSTR lpPassword)
97 {
98 SC_HANDLE hSCManager;
99 SC_HANDLE hSc;
100 SC_LOCK scLock;
101 BOOL bRet = FALSE;
102
103 hSCManager = OpenSCManager(NULL,
104 NULL,
105 SC_MANAGER_LOCK);
106 if (hSCManager)
107 {
108 scLock = LockServiceDatabase(hSCManager);
109 if (scLock)
110 {
111 hSc = OpenService(hSCManager,
112 lpServiceName,
113 SERVICE_CHANGE_CONFIG);
114 if (hSc)
115 {
116 if (ChangeServiceConfig(hSc,
117 pServiceConfig->dwServiceType,
118 pServiceConfig->dwStartType,
119 pServiceConfig->dwErrorControl,
120 pServiceConfig->lpBinaryPathName,
121 pServiceConfig->lpLoadOrderGroup,
122 pServiceConfig->dwTagId ? &pServiceConfig->dwTagId : NULL,
123 pServiceConfig->lpDependencies,
124 pServiceConfig->lpServiceStartName,
125 lpPassword,
126 pServiceConfig->lpDisplayName))
127 {
128 bRet = TRUE;
129 }
130
131 CloseServiceHandle(hSc);
132 }
133
134 UnlockServiceDatabase(scLock);
135 }
136
137 CloseServiceHandle(hSCManager);
138 }
139
140 if (!bRet)
141 GetError();
142
143 return bRet;
144 }
145
146
147 LPTSTR
148 GetServiceDescription(LPTSTR lpServiceName)
149 {
150 SC_HANDLE hSCManager = NULL;
151 SC_HANDLE hSc = NULL;
152 SERVICE_DESCRIPTION *pServiceDescription = NULL;
153 LPTSTR lpDescription = NULL;
154 DWORD BytesNeeded = 0;
155
156 hSCManager = OpenSCManager(NULL,
157 NULL,
158 SC_MANAGER_ENUMERATE_SERVICE);
159 if (hSCManager == NULL)
160 {
161 GetError();
162 return NULL;
163 }
164
165 hSc = OpenService(hSCManager,
166 lpServiceName,
167 SERVICE_QUERY_CONFIG);
168 if (hSc)
169 {
170 if (!QueryServiceConfig2(hSc,
171 SERVICE_CONFIG_DESCRIPTION,
172 NULL,
173 0,
174 &BytesNeeded))
175 {
176 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
177 {
178 pServiceDescription = (SERVICE_DESCRIPTION *) HeapAlloc(ProcessHeap,
179 0,
180 BytesNeeded);
181 if (pServiceDescription == NULL)
182 goto cleanup;
183
184 if (QueryServiceConfig2(hSc,
185 SERVICE_CONFIG_DESCRIPTION,
186 (LPBYTE)pServiceDescription,
187 BytesNeeded,
188 &BytesNeeded))
189 {
190 if (pServiceDescription->lpDescription)
191 {
192 lpDescription = HeapAlloc(ProcessHeap,
193 0,
194 (_tcslen(pServiceDescription->lpDescription) + 1) * sizeof(TCHAR));
195 if (lpDescription)
196 _tcscpy(lpDescription,
197 pServiceDescription->lpDescription);
198 }
199 }
200 }
201 }
202 }
203
204 cleanup:
205 if (pServiceDescription)
206 HeapFree(ProcessHeap,
207 0,
208 pServiceDescription);
209 if (hSCManager != NULL)
210 CloseServiceHandle(hSCManager);
211 if (hSc != NULL)
212 CloseServiceHandle(hSc);
213
214 return lpDescription;
215 }
216
217
218 BOOL
219 SetServiceDescription(LPTSTR lpServiceName,
220 LPTSTR lpDescription)
221 {
222 SC_HANDLE hSCManager;
223 SC_HANDLE hSc;
224 SC_LOCK scLock;
225 SERVICE_DESCRIPTION ServiceDescription;
226 BOOL bRet = FALSE;
227
228 hSCManager = OpenSCManager(NULL,
229 NULL,
230 SC_MANAGER_LOCK);
231 if (hSCManager)
232 {
233 scLock = LockServiceDatabase(hSCManager);
234 if (scLock)
235 {
236 hSc = OpenService(hSCManager,
237 lpServiceName,
238 SERVICE_CHANGE_CONFIG);
239 if (hSc)
240 {
241 ServiceDescription.lpDescription = lpDescription;
242
243 if (ChangeServiceConfig2(hSc,
244 SERVICE_CONFIG_DESCRIPTION,
245 &ServiceDescription))
246 {
247 bRet = TRUE;
248 }
249
250 CloseServiceHandle(hSc);
251 }
252
253 UnlockServiceDatabase(scLock);
254 }
255
256 CloseServiceHandle(hSCManager);
257 }
258
259 if (!bRet)
260 GetError();
261
262 return bRet;
263 }
264
265
266 BOOL
267 GetServiceList(PMAIN_WND_INFO Info,
268 DWORD *NumServices)
269 {
270 SC_HANDLE ScHandle;
271 BOOL bRet = FALSE;
272
273 DWORD BytesNeeded = 0;
274 DWORD ResumeHandle = 0;
275
276 *NumServices = 0;
277
278 if (Info->pAllServices != NULL)
279 {
280 HeapFree(ProcessHeap,
281 0,
282 Info->pAllServices);
283 }
284
285 ScHandle = OpenSCManager(NULL,
286 NULL,
287 SC_MANAGER_ENUMERATE_SERVICE);
288 if (ScHandle != INVALID_HANDLE_VALUE)
289 {
290 if (!EnumServicesStatusEx(ScHandle,
291 SC_ENUM_PROCESS_INFO,
292 SERVICE_WIN32,
293 SERVICE_STATE_ALL,
294 NULL,
295 0,
296 &BytesNeeded,
297 NumServices,
298 &ResumeHandle,
299 0))
300 {
301 /* Call function again if required size was returned */
302 if (GetLastError() == ERROR_MORE_DATA)
303 {
304 /* reserve memory for service info array */
305 Info->pAllServices = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(ProcessHeap,
306 0,
307 BytesNeeded);
308 if (Info->pAllServices)
309 {
310 /* fill array with service info */
311 if (EnumServicesStatusEx(ScHandle,
312 SC_ENUM_PROCESS_INFO,
313 SERVICE_WIN32,
314 SERVICE_STATE_ALL,
315 (LPBYTE)Info->pAllServices,
316 BytesNeeded,
317 &BytesNeeded,
318 NumServices,
319 &ResumeHandle,
320 0))
321 {
322 bRet = TRUE;
323 }
324 }
325 }
326 }
327 }
328
329 if (ScHandle)
330 CloseServiceHandle(ScHandle);
331
332 if (!bRet)
333 {
334 HeapFree(ProcessHeap,
335 0,
336 Info->pAllServices);
337 }
338
339 return bRet;
340 }
341
342
343 BOOL
344 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS* pService)
345 {
346 SC_HANDLE hScm;
347 BOOL bRet = FALSE;
348
349 hScm = OpenSCManager(NULL,
350 NULL,
351 SC_MANAGER_ENUMERATE_SERVICE);
352 if (hScm != INVALID_HANDLE_VALUE)
353 {
354 SC_HANDLE hService;
355
356 hService = OpenService(hScm,
357 pService->lpServiceName,
358 SERVICE_QUERY_STATUS);
359 if (hService)
360 {
361 DWORD size;
362
363 QueryServiceStatusEx(hService,
364 SC_STATUS_PROCESS_INFO,
365 (LPBYTE)&pService->ServiceStatusProcess,
366 sizeof(SERVICE_STATUS_PROCESS),
367 &size);
368
369 CloseServiceHandle(hService);
370 bRet = TRUE;
371 }
372
373 CloseServiceHandle(hScm);
374 }
375
376 return bRet;
377 }