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>
12 ENUM_SERVICE_STATUS_PROCESS
*
13 GetSelectedService(PMAIN_WND_INFO Info
)
17 lvItem
.mask
= LVIF_PARAM
;
18 lvItem
.iItem
= Info
->SelectedItem
;
19 SendMessage(Info
->hListView
,
24 /* return pointer to selected service */
25 return (ENUM_SERVICE_STATUS_PROCESS
*)lvItem
.lParam
;
28 LPQUERY_SERVICE_CONFIG
29 GetServiceConfig(LPWSTR lpServiceName
)
31 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= NULL
;
36 hSCManager
= OpenSCManagerW(NULL
,
41 hService
= OpenServiceW(hSCManager
,
43 SERVICE_QUERY_CONFIG
);
46 if (!QueryServiceConfigW(hService
,
51 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
53 lpServiceConfig
= (LPQUERY_SERVICE_CONFIG
)HeapAlloc(GetProcessHeap(),
58 if (!QueryServiceConfigW(hService
,
63 HeapFree(GetProcessHeap(),
66 lpServiceConfig
= NULL
;
72 CloseServiceHandle(hService
);
75 CloseServiceHandle(hSCManager
);
78 return lpServiceConfig
;
82 SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig
,
91 hSCManager
= OpenSCManagerW(NULL
,
96 scLock
= LockServiceDatabase(hSCManager
);
99 hSc
= OpenServiceW(hSCManager
,
101 SERVICE_CHANGE_CONFIG
);
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
,
114 pServiceConfig
->lpDisplayName
))
119 CloseServiceHandle(hSc
);
122 UnlockServiceDatabase(scLock
);
125 CloseServiceHandle(hSCManager
);
135 GetServiceDescription(LPWSTR lpServiceName
)
137 SC_HANDLE hSCManager
= NULL
;
138 SC_HANDLE hSc
= NULL
;
139 SERVICE_DESCRIPTIONW
*pServiceDescription
= NULL
;
140 LPWSTR lpDescription
= NULL
;
141 DWORD BytesNeeded
= 0;
144 hSCManager
= OpenSCManagerW(NULL
,
146 SC_MANAGER_ENUMERATE_SERVICE
);
147 if (hSCManager
== NULL
)
153 hSc
= OpenServiceW(hSCManager
,
155 SERVICE_QUERY_CONFIG
);
158 if (!QueryServiceConfig2W(hSc
,
159 SERVICE_CONFIG_DESCRIPTION
,
164 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
166 pServiceDescription
= (SERVICE_DESCRIPTION
*) HeapAlloc(ProcessHeap
,
169 if (pServiceDescription
== NULL
)
172 if (QueryServiceConfig2W(hSc
,
173 SERVICE_CONFIG_DESCRIPTION
,
174 (LPBYTE
)pServiceDescription
,
178 if (pServiceDescription
->lpDescription
)
180 dwSize
= wcslen(pServiceDescription
->lpDescription
) + 1;
181 lpDescription
= HeapAlloc(ProcessHeap
,
183 dwSize
* sizeof(WCHAR
));
186 StringCchCopyW(lpDescription
,
188 pServiceDescription
->lpDescription
);
197 if (pServiceDescription
)
198 HeapFree(ProcessHeap
,
200 pServiceDescription
);
201 if (hSCManager
!= NULL
)
202 CloseServiceHandle(hSCManager
);
204 CloseServiceHandle(hSc
);
206 return lpDescription
;
210 SetServiceDescription(LPWSTR lpServiceName
,
211 LPWSTR lpDescription
)
213 SC_HANDLE hSCManager
;
216 SERVICE_DESCRIPTION ServiceDescription
;
219 hSCManager
= OpenSCManagerW(NULL
,
224 scLock
= LockServiceDatabase(hSCManager
);
227 hSc
= OpenServiceW(hSCManager
,
229 SERVICE_CHANGE_CONFIG
);
232 ServiceDescription
.lpDescription
= lpDescription
;
234 if (ChangeServiceConfig2W(hSc
,
235 SERVICE_CONFIG_DESCRIPTION
,
236 &ServiceDescription
))
241 CloseServiceHandle(hSc
);
244 UnlockServiceDatabase(scLock
);
247 CloseServiceHandle(hSCManager
);
257 FreeServiceList(PMAIN_WND_INFO Info
)
261 if (Info
->pAllServices
!= NULL
)
263 for (i
= 0; i
< Info
->NumServices
; i
++)
265 if (Info
->pAllServices
[i
].lpServiceName
)
266 HeapFree(ProcessHeap
, 0, Info
->pAllServices
[i
].lpServiceName
);
268 if (Info
->pAllServices
[i
].lpDisplayName
)
269 HeapFree(ProcessHeap
, 0, Info
->pAllServices
[i
].lpDisplayName
);
272 HeapFree(ProcessHeap
, 0, Info
->pAllServices
);
273 Info
->pAllServices
= NULL
;
274 Info
->NumServices
= 0;
279 GetServiceList(PMAIN_WND_INFO Info
)
281 ENUM_SERVICE_STATUS_PROCESS
*pServices
= NULL
;
285 DWORD BytesNeeded
= 0;
286 DWORD ResumeHandle
= 0;
287 DWORD NumServices
= 0;
290 FreeServiceList(Info
);
292 ScHandle
= OpenSCManagerW(NULL
,
294 SC_MANAGER_ENUMERATE_SERVICE
);
295 if (ScHandle
!= NULL
)
297 if (!EnumServicesStatusEx(ScHandle
,
298 SC_ENUM_PROCESS_INFO
,
308 /* Call function again if required size was returned */
309 if (GetLastError() == ERROR_MORE_DATA
)
311 /* reserve memory for service info array */
312 pServices
= (ENUM_SERVICE_STATUS_PROCESS
*)HeapAlloc(ProcessHeap
,
317 /* fill array with service info */
318 if (EnumServicesStatusEx(ScHandle
,
319 SC_ENUM_PROCESS_INFO
,
337 CloseServiceHandle(ScHandle
);
339 Info
->pAllServices
= (ENUM_SERVICE_STATUS_PROCESS
*)HeapAlloc(ProcessHeap
,
341 NumServices
* sizeof(ENUM_SERVICE_STATUS_PROCESS
));
342 if (Info
->pAllServices
!= NULL
)
344 Info
->NumServices
= NumServices
;
346 for (i
= 0; i
< NumServices
; i
++)
348 Info
->pAllServices
[i
].lpServiceName
= HeapAlloc(ProcessHeap
,
350 (wcslen(pServices
[i
].lpServiceName
) + 1) * sizeof(WCHAR
));
351 if (Info
->pAllServices
[i
].lpServiceName
)
352 wcscpy(Info
->pAllServices
[i
].lpServiceName
, pServices
[i
].lpServiceName
);
354 Info
->pAllServices
[i
].lpDisplayName
= HeapAlloc(ProcessHeap
,
356 (wcslen(pServices
[i
].lpDisplayName
) + 1) * sizeof(WCHAR
));
357 if (Info
->pAllServices
[i
].lpDisplayName
)
358 wcscpy(Info
->pAllServices
[i
].lpDisplayName
, pServices
[i
].lpDisplayName
);
360 CopyMemory(&Info
->pAllServices
[i
].ServiceStatusProcess
,
361 &pServices
[i
].ServiceStatusProcess
,
362 sizeof(SERVICE_STATUS_PROCESS
));
367 HeapFree(ProcessHeap
, 0, pServices
);
373 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS
* pService
)
378 hScm
= OpenSCManagerW(NULL
,
380 SC_MANAGER_ENUMERATE_SERVICE
);
385 hService
= OpenServiceW(hScm
,
386 pService
->lpServiceName
,
387 SERVICE_QUERY_STATUS
);
392 QueryServiceStatusEx(hService
,
393 SC_STATUS_PROCESS_INFO
,
394 (LPBYTE
)&pService
->ServiceStatusProcess
,
395 sizeof(SERVICE_STATUS_PROCESS
),
398 CloseServiceHandle(hService
);
402 CloseServiceHandle(hScm
);