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>
13 ENUM_SERVICE_STATUS_PROCESS
*
14 GetSelectedService(PMAIN_WND_INFO Info
)
18 lvItem
.mask
= LVIF_PARAM
;
19 lvItem
.iItem
= Info
->SelectedItem
;
20 SendMessage(Info
->hListView
,
25 /* return pointer to selected service */
26 return (ENUM_SERVICE_STATUS_PROCESS
*)lvItem
.lParam
;
30 LPQUERY_SERVICE_CONFIG
31 GetServiceConfig(LPTSTR lpServiceName
)
33 SC_HANDLE hSCManager
= NULL
;
35 LPQUERY_SERVICE_CONFIG pServiceConfig
= NULL
;
36 DWORD BytesNeeded
= 0;
38 hSCManager
= OpenSCManager(NULL
,
40 SC_MANAGER_ENUMERATE_SERVICE
);
41 if (hSCManager
== NULL
)
47 hSc
= OpenService(hSCManager
,
49 SERVICE_QUERY_CONFIG
);
56 if (!QueryServiceConfig(hSc
,
61 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
63 pServiceConfig
= (LPQUERY_SERVICE_CONFIG
) HeapAlloc(ProcessHeap
,
66 if (pServiceConfig
== NULL
)
69 if (!QueryServiceConfig(hSc
,
78 pServiceConfig
= NULL
;
84 if (hSCManager
!= NULL
)
85 CloseServiceHandle(hSCManager
);
87 CloseServiceHandle(hSc
);
89 return pServiceConfig
;
94 SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig
,
103 hSCManager
= OpenSCManager(NULL
,
108 scLock
= LockServiceDatabase(hSCManager
);
111 hSc
= OpenService(hSCManager
,
113 SERVICE_CHANGE_CONFIG
);
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
,
126 pServiceConfig
->lpDisplayName
))
131 CloseServiceHandle(hSc
);
134 UnlockServiceDatabase(scLock
);
137 CloseServiceHandle(hSCManager
);
148 GetServiceDescription(LPTSTR lpServiceName
)
150 SC_HANDLE hSCManager
= NULL
;
151 SC_HANDLE hSc
= NULL
;
152 SERVICE_DESCRIPTION
*pServiceDescription
= NULL
;
153 LPTSTR lpDescription
= NULL
;
154 DWORD BytesNeeded
= 0;
156 hSCManager
= OpenSCManager(NULL
,
158 SC_MANAGER_ENUMERATE_SERVICE
);
159 if (hSCManager
== NULL
)
165 hSc
= OpenService(hSCManager
,
167 SERVICE_QUERY_CONFIG
);
170 if (!QueryServiceConfig2(hSc
,
171 SERVICE_CONFIG_DESCRIPTION
,
176 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
178 pServiceDescription
= (SERVICE_DESCRIPTION
*) HeapAlloc(ProcessHeap
,
181 if (pServiceDescription
== NULL
)
184 if (QueryServiceConfig2(hSc
,
185 SERVICE_CONFIG_DESCRIPTION
,
186 (LPBYTE
)pServiceDescription
,
190 if (pServiceDescription
->lpDescription
)
192 lpDescription
= HeapAlloc(ProcessHeap
,
194 (_tcslen(pServiceDescription
->lpDescription
) + 1) * sizeof(TCHAR
));
196 _tcscpy(lpDescription
,
197 pServiceDescription
->lpDescription
);
205 if (pServiceDescription
)
206 HeapFree(ProcessHeap
,
208 pServiceDescription
);
209 if (hSCManager
!= NULL
)
210 CloseServiceHandle(hSCManager
);
212 CloseServiceHandle(hSc
);
214 return lpDescription
;
219 SetServiceDescription(LPTSTR lpServiceName
,
220 LPTSTR lpDescription
)
222 SC_HANDLE hSCManager
;
225 SERVICE_DESCRIPTION ServiceDescription
;
228 hSCManager
= OpenSCManager(NULL
,
233 scLock
= LockServiceDatabase(hSCManager
);
236 hSc
= OpenService(hSCManager
,
238 SERVICE_CHANGE_CONFIG
);
241 ServiceDescription
.lpDescription
= lpDescription
;
243 if (ChangeServiceConfig2(hSc
,
244 SERVICE_CONFIG_DESCRIPTION
,
245 &ServiceDescription
))
250 CloseServiceHandle(hSc
);
253 UnlockServiceDatabase(scLock
);
256 CloseServiceHandle(hSCManager
);
267 GetServiceList(PMAIN_WND_INFO Info
,
273 DWORD BytesNeeded
= 0;
274 DWORD ResumeHandle
= 0;
278 if (Info
->pAllServices
!= NULL
)
280 HeapFree(ProcessHeap
,
285 ScHandle
= OpenSCManager(NULL
,
287 SC_MANAGER_ENUMERATE_SERVICE
);
288 if (ScHandle
!= INVALID_HANDLE_VALUE
)
290 if (!EnumServicesStatusEx(ScHandle
,
291 SC_ENUM_PROCESS_INFO
,
301 /* Call function again if required size was returned */
302 if (GetLastError() == ERROR_MORE_DATA
)
304 /* reserve memory for service info array */
305 Info
->pAllServices
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(ProcessHeap
,
308 if (Info
->pAllServices
)
310 /* fill array with service info */
311 if (EnumServicesStatusEx(ScHandle
,
312 SC_ENUM_PROCESS_INFO
,
315 (LPBYTE
)Info
->pAllServices
,
330 CloseServiceHandle(ScHandle
);
334 HeapFree(ProcessHeap
,
344 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS
* pService
)
349 hScm
= OpenSCManager(NULL
,
351 SC_MANAGER_ENUMERATE_SERVICE
);
352 if (hScm
!= INVALID_HANDLE_VALUE
)
356 hService
= OpenService(hScm
,
357 pService
->lpServiceName
,
358 SERVICE_QUERY_STATUS
);
363 QueryServiceStatusEx(hService
,
364 SC_STATUS_PROCESS_INFO
,
365 (LPBYTE
)&pService
->ServiceStatusProcess
,
366 sizeof(SERVICE_STATUS_PROCESS
),
369 CloseServiceHandle(hService
);
373 CloseServiceHandle(hScm
);