ReactOS  0.4.12-dev-51-ge94618b
query.c
Go to the documentation of this file.
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 
14 {
15  LVITEM lvItem;
16 
17  lvItem.mask = LVIF_PARAM;
18  lvItem.iItem = Info->SelectedItem;
19  SendMessage(Info->hListView,
21  0,
22  (LPARAM)&lvItem);
23 
24  /* return pointer to selected service */
25  return (ENUM_SERVICE_STATUS_PROCESS *)lvItem.lParam;
26 }
27 
29 GetServiceConfig(LPWSTR lpServiceName)
30 {
31  LPQUERY_SERVICE_CONFIGW lpServiceConfig = NULL;
32  SC_HANDLE hSCManager;
33  SC_HANDLE hService;
34  DWORD dwBytesNeeded;
35 
36  hSCManager = OpenSCManagerW(NULL,
37  NULL,
39  if (hSCManager)
40  {
41  hService = OpenServiceW(hSCManager,
42  lpServiceName,
44  if (hService)
45  {
46  if (!QueryServiceConfigW(hService,
47  NULL,
48  0,
49  &dwBytesNeeded))
50  {
52  {
53  lpServiceConfig = (LPQUERY_SERVICE_CONFIG)HeapAlloc(GetProcessHeap(),
54  0,
55  dwBytesNeeded);
56  if (lpServiceConfig)
57  {
58  if (!QueryServiceConfigW(hService,
59  lpServiceConfig,
60  dwBytesNeeded,
61  &dwBytesNeeded))
62  {
64  0,
65  lpServiceConfig);
66  lpServiceConfig = NULL;
67  }
68  }
69  }
70  }
71 
72  CloseServiceHandle(hService);
73  }
74 
75  CloseServiceHandle(hSCManager);
76  }
77 
78  return lpServiceConfig;
79 }
80 
81 BOOL
83  LPWSTR lpServiceName,
84  LPWSTR lpPassword)
85 {
86  SC_HANDLE hSCManager;
87  SC_HANDLE hSc;
88  SC_LOCK scLock;
89  BOOL bRet = FALSE;
90 
91  hSCManager = OpenSCManagerW(NULL,
92  NULL,
94  if (hSCManager)
95  {
96  scLock = LockServiceDatabase(hSCManager);
97  if (scLock)
98  {
99  hSc = OpenServiceW(hSCManager,
100  lpServiceName,
102  if (hSc)
103  {
104  if (ChangeServiceConfigW(hSc,
105  pServiceConfig->dwServiceType,
106  pServiceConfig->dwStartType,
107  pServiceConfig->dwErrorControl,
108  pServiceConfig->lpBinaryPathName,
109  pServiceConfig->lpLoadOrderGroup,
110  pServiceConfig->dwTagId ? &pServiceConfig->dwTagId : NULL,
111  pServiceConfig->lpDependencies,
112  pServiceConfig->lpServiceStartName,
113  lpPassword,
114  pServiceConfig->lpDisplayName))
115  {
116  bRet = TRUE;
117  }
118 
119  CloseServiceHandle(hSc);
120  }
121 
122  UnlockServiceDatabase(scLock);
123  }
124 
125  CloseServiceHandle(hSCManager);
126  }
127 
128  if (!bRet)
129  GetError();
130 
131  return bRet;
132 }
133 
134 LPWSTR
136 {
137  SC_HANDLE hSCManager = NULL;
138  SC_HANDLE hSc = NULL;
139  SERVICE_DESCRIPTIONW *pServiceDescription = NULL;
140  LPWSTR lpDescription = NULL;
141  DWORD BytesNeeded = 0;
142  DWORD dwSize;
143 
144  hSCManager = OpenSCManagerW(NULL,
145  NULL,
147  if (hSCManager == NULL)
148  {
149  GetError();
150  return NULL;
151  }
152 
153  hSc = OpenServiceW(hSCManager,
154  lpServiceName,
156  if (hSc)
157  {
158  if (!QueryServiceConfig2W(hSc,
160  NULL,
161  0,
162  &BytesNeeded))
163  {
165  {
166  pServiceDescription = (SERVICE_DESCRIPTION *) HeapAlloc(ProcessHeap,
167  0,
168  BytesNeeded);
169  if (pServiceDescription == NULL)
170  goto cleanup;
171 
172  if (QueryServiceConfig2W(hSc,
174  (LPBYTE)pServiceDescription,
175  BytesNeeded,
176  &BytesNeeded))
177  {
178  if (pServiceDescription->lpDescription)
179  {
180  dwSize = wcslen(pServiceDescription->lpDescription) + 1;
181  lpDescription = HeapAlloc(ProcessHeap,
182  0,
183  dwSize * sizeof(WCHAR));
184  if (lpDescription)
185  {
186  StringCchCopyW(lpDescription,
187  dwSize,
188  pServiceDescription->lpDescription);
189  }
190  }
191  }
192  }
193  }
194  }
195 
196 cleanup:
197  if (pServiceDescription)
199  0,
200  pServiceDescription);
201  if (hSCManager != NULL)
202  CloseServiceHandle(hSCManager);
203  if (hSc != NULL)
204  CloseServiceHandle(hSc);
205 
206  return lpDescription;
207 }
208 
209 BOOL
211  LPWSTR lpDescription)
212 {
213  SC_HANDLE hSCManager;
214  SC_HANDLE hSc;
215  SC_LOCK scLock;
216  SERVICE_DESCRIPTION ServiceDescription;
217  BOOL bRet = FALSE;
218 
219  hSCManager = OpenSCManagerW(NULL,
220  NULL,
222  if (hSCManager)
223  {
224  scLock = LockServiceDatabase(hSCManager);
225  if (scLock)
226  {
227  hSc = OpenServiceW(hSCManager,
228  lpServiceName,
230  if (hSc)
231  {
232  ServiceDescription.lpDescription = lpDescription;
233 
234  if (ChangeServiceConfig2W(hSc,
236  &ServiceDescription))
237  {
238  bRet = TRUE;
239  }
240 
241  CloseServiceHandle(hSc);
242  }
243 
244  UnlockServiceDatabase(scLock);
245  }
246 
247  CloseServiceHandle(hSCManager);
248  }
249 
250  if (!bRet)
251  GetError();
252 
253  return bRet;
254 }
255 
256 VOID
258 {
259  DWORD i;
260 
261  if (Info->pAllServices != NULL)
262  {
263  for (i = 0; i < Info->NumServices; i++)
264  {
265  if (Info->pAllServices[i].lpServiceName)
267 
268  if (Info->pAllServices[i].lpDisplayName)
270  }
271 
272  HeapFree(ProcessHeap, 0, Info->pAllServices);
273  Info->pAllServices = NULL;
274  Info->NumServices = 0;
275  }
276 }
277 
278 BOOL
280 {
281  ENUM_SERVICE_STATUS_PROCESS *pServices = NULL;
282  SC_HANDLE ScHandle;
283  BOOL bRet = FALSE;
284 
285  DWORD BytesNeeded = 0;
286  DWORD ResumeHandle = 0;
287  DWORD NumServices = 0;
288  DWORD i;
289 
290  FreeServiceList(Info);
291 
292  ScHandle = OpenSCManagerW(NULL,
293  NULL,
295  if (ScHandle != NULL)
296  {
297  if (!EnumServicesStatusEx(ScHandle,
301  NULL,
302  0,
303  &BytesNeeded,
304  &NumServices,
305  &ResumeHandle,
306  0))
307  {
308  /* Call function again if required size was returned */
309  if (GetLastError() == ERROR_MORE_DATA)
310  {
311  /* reserve memory for service info array */
313  0,
314  BytesNeeded);
315  if (pServices)
316  {
317  /* fill array with service info */
318  if (EnumServicesStatusEx(ScHandle,
322  (LPBYTE)pServices,
323  BytesNeeded,
324  &BytesNeeded,
325  &NumServices,
326  &ResumeHandle,
327  0))
328  {
329  bRet = TRUE;
330  }
331  }
332  }
333  }
334  }
335 
336  if (ScHandle)
337  CloseServiceHandle(ScHandle);
338 
341  NumServices * sizeof(ENUM_SERVICE_STATUS_PROCESS));
342  if (Info->pAllServices != NULL)
343  {
344  Info->NumServices = NumServices;
345 
346  for (i = 0; i < NumServices; i++)
347  {
350  (wcslen(pServices[i].lpServiceName) + 1) * sizeof(WCHAR));
351  if (Info->pAllServices[i].lpServiceName)
352  wcscpy(Info->pAllServices[i].lpServiceName, pServices[i].lpServiceName);
353 
356  (wcslen(pServices[i].lpDisplayName) + 1) * sizeof(WCHAR));
357  if (Info->pAllServices[i].lpDisplayName)
358  wcscpy(Info->pAllServices[i].lpDisplayName, pServices[i].lpDisplayName);
359 
361  &pServices[i].ServiceStatusProcess,
362  sizeof(SERVICE_STATUS_PROCESS));
363  }
364  }
365 
366  if (pServices)
367  HeapFree(ProcessHeap, 0, pServices);
368 
369  return bRet;
370 }
371 
372 BOOL
374 {
375  SC_HANDLE hScm;
376  BOOL bRet = FALSE;
377 
378  hScm = OpenSCManagerW(NULL,
379  NULL,
381  if (hScm != NULL)
382  {
383  SC_HANDLE hService;
384 
385  hService = OpenServiceW(hScm,
386  pService->lpServiceName,
388  if (hService)
389  {
390  DWORD size;
391 
392  QueryServiceStatusEx(hService,
394  (LPBYTE)&pService->ServiceStatusProcess,
395  sizeof(SERVICE_STATUS_PROCESS),
396  &size);
397 
398  CloseServiceHandle(hService);
399  bRet = TRUE;
400  }
401 
402  CloseServiceHandle(hScm);
403  }
404 
405  return bRet;
406 }
#define SC_MANAGER_ENUMERATE_SERVICE
Definition: winsvc.h:16
#define TRUE
Definition: types.h:120
QUERY_SERVICE_CONFIGA * LPQUERY_SERVICE_CONFIG
Definition: winsvc.h:550
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
LPSTR lpServiceStartName
Definition: winsvc.h:152
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL WINAPI QueryServiceConfigW(SC_HANDLE hService, LPQUERY_SERVICE_CONFIGW lpServiceConfig, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2233
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL WINAPI UnlockServiceDatabase(SC_LOCK ScLock)
Definition: scm.c:2960
#define SERVICE_CONFIG_DESCRIPTION
Definition: winsvc.h:65
#define SERVICE_STATE_ALL
Definition: winsvc.h:52
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2829
BOOL GetServiceList(PMAIN_WND_INFO Info)
Definition: query.c:279
struct TraceInfo Info
INT SelectedItem
Definition: precomp.h:60
DWORD NumServices
Definition: precomp.h:58
VOID GetError(VOID)
Definition: misc.c:192
GLenum GLclampf GLint i
Definition: glfuncs.h:14
LPWSTR GetServiceDescription(LPWSTR lpServiceName)
Definition: query.c:135
BOOL SetServiceDescription(LPWSTR lpServiceName, LPWSTR lpDescription)
Definition: query.c:210
unsigned char * LPBYTE
Definition: typedefs.h:52
LONG_PTR LPARAM
Definition: windef.h:208
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
BOOL WINAPI ChangeServiceConfigW(SC_HANDLE hService, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCWSTR lpBinaryPathName, LPCWSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCWSTR lpDependencies, LPCWSTR lpServiceStartName, LPCWSTR lpPassword, LPCWSTR lpDisplayName)
Definition: scm.c:479
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2102
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizeiptr size
Definition: glext.h:5919
ENUM_SERVICE_STATUS_PROCESS * pAllServices
Definition: precomp.h:56
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:54
#define CopyMemory
Definition: winbase.h:1633
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SC_MANAGER_LOCK
Definition: winsvc.h:17
LPQUERY_SERVICE_CONFIG GetServiceConfig(LPWSTR lpServiceName)
Definition: query.c:29
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
ENUM_SERVICE_STATUS_PROCESS * GetSelectedService(PMAIN_WND_INFO Info)
Definition: query.c:13
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2010
BOOL WINAPI QueryServiceConfig2W(SC_HANDLE hService, DWORD dwInfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2433
BOOL SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig, LPWSTR lpServiceName, LPWSTR lpPassword)
Definition: query.c:82
SC_HANDLE hSCManager
Definition: sc.c:12
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define LVM_GETITEM
Definition: commctrl.h:2362
HANDLE ProcessHeap
Definition: servman.c:15
#define SERVICE_WIN32
Definition: cmtypes.h:962
SC_LOCK WINAPI LockServiceDatabase(SC_HANDLE hSCManager)
Definition: scm.c:1900
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
#define EnumServicesStatusEx
Definition: winsvc.h:572
#define SendMessage
Definition: winuser.h:5709
#define LVIF_PARAM
Definition: commctrl.h:2281
#define LVITEM
Definition: commctrl.h:2345
char * cleanup(char *str)
Definition: wpickclick.c:99
#define SERVICE_QUERY_CONFIG
Definition: winsvc.h:53
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL WINAPI ChangeServiceConfig2W(SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo)
Definition: scm.c:303
SERVICE_STATUS_PROCESS ServiceStatusProcess
Definition: winsvc.h:137
_In_ LPCSTR _Out_writes_to_opt_ cchDisplayName LPSTR lpDisplayName
Definition: winbase.h:2730
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
HWND hListView
Definition: precomp.h:49
BOOL UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS *pService)
Definition: query.c:373
VOID FreeServiceList(PMAIN_WND_INFO Info)
Definition: query.c:257
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10