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 LPQUERY_SERVICE_CONFIG lpServiceConfig
= NULL
;
38 hSCManager
= OpenSCManager(NULL
,
40 SC_MANAGER_ALL_ACCESS
);
43 hService
= OpenService(hSCManager
,
45 SERVICE_QUERY_STATUS
| SERVICE_ENUMERATE_DEPENDENTS
| SERVICE_QUERY_CONFIG
);
48 if (!QueryServiceConfig(hService
,
53 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
55 lpServiceConfig
= (LPQUERY_SERVICE_CONFIG
)HeapAlloc(GetProcessHeap(),
60 if (!QueryServiceConfig(hService
,
65 HeapFree(GetProcessHeap(),
68 lpServiceConfig
= NULL
;
74 CloseServiceHandle(hService
);
77 CloseServiceHandle(hSCManager
);
80 return lpServiceConfig
;
85 SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig
,
94 hSCManager
= OpenSCManager(NULL
,
99 scLock
= LockServiceDatabase(hSCManager
);
102 hSc
= OpenService(hSCManager
,
104 SERVICE_CHANGE_CONFIG
);
107 if (ChangeServiceConfig(hSc
,
108 pServiceConfig
->dwServiceType
,
109 pServiceConfig
->dwStartType
,
110 pServiceConfig
->dwErrorControl
,
111 pServiceConfig
->lpBinaryPathName
,
112 pServiceConfig
->lpLoadOrderGroup
,
113 pServiceConfig
->dwTagId
? &pServiceConfig
->dwTagId
: NULL
,
114 pServiceConfig
->lpDependencies
,
115 pServiceConfig
->lpServiceStartName
,
117 pServiceConfig
->lpDisplayName
))
122 CloseServiceHandle(hSc
);
125 UnlockServiceDatabase(scLock
);
128 CloseServiceHandle(hSCManager
);
139 GetServiceDescription(LPTSTR lpServiceName
)
141 SC_HANDLE hSCManager
= NULL
;
142 SC_HANDLE hSc
= NULL
;
143 SERVICE_DESCRIPTION
*pServiceDescription
= NULL
;
144 LPTSTR lpDescription
= NULL
;
145 DWORD BytesNeeded
= 0;
148 hSCManager
= OpenSCManager(NULL
,
150 SC_MANAGER_ENUMERATE_SERVICE
);
151 if (hSCManager
== NULL
)
157 hSc
= OpenService(hSCManager
,
159 SERVICE_QUERY_CONFIG
);
162 if (!QueryServiceConfig2(hSc
,
163 SERVICE_CONFIG_DESCRIPTION
,
168 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
170 pServiceDescription
= (SERVICE_DESCRIPTION
*) HeapAlloc(ProcessHeap
,
173 if (pServiceDescription
== NULL
)
176 if (QueryServiceConfig2(hSc
,
177 SERVICE_CONFIG_DESCRIPTION
,
178 (LPBYTE
)pServiceDescription
,
182 if (pServiceDescription
->lpDescription
)
184 dwSize
= _tcslen(pServiceDescription
->lpDescription
) + 1;
185 lpDescription
= HeapAlloc(ProcessHeap
,
187 dwSize
* sizeof(TCHAR
));
190 _tcscpy_s(lpDescription
,
192 pServiceDescription
->lpDescription
);
201 if (pServiceDescription
)
202 HeapFree(ProcessHeap
,
204 pServiceDescription
);
205 if (hSCManager
!= NULL
)
206 CloseServiceHandle(hSCManager
);
208 CloseServiceHandle(hSc
);
210 return lpDescription
;
215 SetServiceDescription(LPTSTR lpServiceName
,
216 LPTSTR lpDescription
)
218 SC_HANDLE hSCManager
;
221 SERVICE_DESCRIPTION ServiceDescription
;
224 hSCManager
= OpenSCManager(NULL
,
229 scLock
= LockServiceDatabase(hSCManager
);
232 hSc
= OpenService(hSCManager
,
234 SERVICE_CHANGE_CONFIG
);
237 ServiceDescription
.lpDescription
= lpDescription
;
239 if (ChangeServiceConfig2(hSc
,
240 SERVICE_CONFIG_DESCRIPTION
,
241 &ServiceDescription
))
246 CloseServiceHandle(hSc
);
249 UnlockServiceDatabase(scLock
);
252 CloseServiceHandle(hSCManager
);
263 GetServiceList(PMAIN_WND_INFO Info
,
269 DWORD BytesNeeded
= 0;
270 DWORD ResumeHandle
= 0;
274 if (Info
->pAllServices
!= NULL
)
276 HeapFree(ProcessHeap
,
281 ScHandle
= OpenSCManager(NULL
,
283 SC_MANAGER_ENUMERATE_SERVICE
);
284 if (ScHandle
!= INVALID_HANDLE_VALUE
)
286 if (!EnumServicesStatusEx(ScHandle
,
287 SC_ENUM_PROCESS_INFO
,
297 /* Call function again if required size was returned */
298 if (GetLastError() == ERROR_MORE_DATA
)
300 /* reserve memory for service info array */
301 Info
->pAllServices
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(ProcessHeap
,
304 if (Info
->pAllServices
)
306 /* fill array with service info */
307 if (EnumServicesStatusEx(ScHandle
,
308 SC_ENUM_PROCESS_INFO
,
311 (LPBYTE
)Info
->pAllServices
,
326 CloseServiceHandle(ScHandle
);
330 HeapFree(ProcessHeap
,
340 UpdateServiceStatus(ENUM_SERVICE_STATUS_PROCESS
* pService
)
345 hScm
= OpenSCManager(NULL
,
347 SC_MANAGER_ENUMERATE_SERVICE
);
348 if (hScm
!= INVALID_HANDLE_VALUE
)
352 hService
= OpenService(hScm
,
353 pService
->lpServiceName
,
354 SERVICE_QUERY_STATUS
);
359 QueryServiceStatusEx(hService
,
360 SC_STATUS_PROCESS_INFO
,
361 (LPBYTE
)&pService
->ServiceStatusProcess
,
362 sizeof(SERVICE_STATUS_PROCESS
),
365 CloseServiceHandle(hService
);
369 CloseServiceHandle(hScm
);