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
,
38 SC_MANAGER_ALL_ACCESS
);
41 hService
= OpenServiceW(hSCManager
,
43 SERVICE_QUERY_STATUS
| SERVICE_ENUMERATE_DEPENDENTS
| 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 GetServiceList(PMAIN_WND_INFO Info
,
263 DWORD BytesNeeded
= 0;
264 DWORD ResumeHandle
= 0;
268 if (Info
->pAllServices
!= NULL
)
270 HeapFree(ProcessHeap
,
273 Info
->pAllServices
= NULL
;
276 ScHandle
= OpenSCManager(NULL
,
278 SC_MANAGER_ENUMERATE_SERVICE
);
279 if (ScHandle
!= INVALID_HANDLE_VALUE
)
281 if (!EnumServicesStatusEx(ScHandle
,
282 SC_ENUM_PROCESS_INFO
,
292 /* Call function again if required size was returned */
293 if (GetLastError() == ERROR_MORE_DATA
)
295 /* reserve memory for service info array */
296 Info
->pAllServices
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(ProcessHeap
,
299 if (Info
->pAllServices
)
301 /* fill array with service info */
302 if (EnumServicesStatusEx(ScHandle
,
303 SC_ENUM_PROCESS_INFO
,
306 (LPBYTE
)Info
->pAllServices
,
321 CloseServiceHandle(ScHandle
);
323 if (!bRet
&& Info
->pAllServices
)
325 HeapFree(ProcessHeap
,
334 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS
* pService
)
339 hScm
= OpenSCManager(NULL
,
341 SC_MANAGER_ENUMERATE_SERVICE
);
342 if (hScm
!= INVALID_HANDLE_VALUE
)
346 hService
= OpenService(hScm
,
347 pService
->lpServiceName
,
348 SERVICE_QUERY_STATUS
);
353 QueryServiceStatusEx(hService
,
354 SC_STATUS_PROCESS_INFO
,
355 (LPBYTE
)&pService
->ServiceStatusProcess
,
356 sizeof(SERVICE_STATUS_PROCESS
),
359 CloseServiceHandle(hService
);
363 CloseServiceHandle(hScm
);