96458353eb40281be5563205d0454fa17b64cf83
[reactos.git] / reactos / subsys / system / servman / query.c
1 #include "servman.h"
2
3 extern HINSTANCE hInstance;
4 extern HWND hListView;
5 extern HWND hStatus;
6
7 /* Stores the service array */
8 ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
9
10
11 /* free service array */
12 VOID FreeMemory(VOID)
13 {
14 HeapFree(GetProcessHeap(), 0, pServiceStatus);
15 }
16
17
18 BOOL
19 RefreshServiceList(VOID)
20 {
21 LV_ITEM item;
22 TCHAR szNumServices[32];
23 TCHAR szStatus[128];
24 DWORD NumServices = 0;
25 DWORD Index;
26
27 NumServices = GetServiceList();
28
29 if (NumServices)
30 {
31 HICON hiconItem; /* icon for list-view items */
32 HIMAGELIST hSmall; /* image list for other views */
33 TCHAR buf[40];
34
35 /* Create the icon image lists */
36 hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
37 GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
38
39 /* Add an icon to each image list */
40 hiconItem = LoadImage(hInstance, MAKEINTRESOURCE(IDI_SM_ICON), IMAGE_ICON, 16, 16, 0);
41 ImageList_AddIcon(hSmall, hiconItem);
42
43 ListView_SetImageList(hListView, hSmall, LVSIL_SMALL);
44
45 /* set the number of services in the status bar */
46 LoadString(hInstance, IDS_SERVICES_NUM_SERVICES, szNumServices, 32);
47 _stprintf(buf, szNumServices, NumServices);
48 SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)buf);
49
50 for (Index = 0; Index < NumServices; Index++)
51 {
52 HKEY hKey = NULL;
53 TCHAR Description[5000];
54 DWORD Size = 5000;
55
56 /* set the display name */
57
58 ZeroMemory(&item, sizeof(LV_ITEM));
59 item.mask = LVIF_TEXT;
60 //item.iImage = 0;
61 item.pszText = pServiceStatus[Index].lpDisplayName;
62 item.iItem = ListView_GetItemCount(hListView);
63 item.lParam = 0;
64 item.iItem = ListView_InsertItem(hListView, &item);
65
66
67 /* set the description */
68
69 _stprintf(buf, _T("System\\CurrentControlSet\\Services\\%s"),
70 pServiceStatus[Index].lpServiceName);
71
72 if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
73 buf,
74 0,
75 KEY_READ,
76 &hKey) != ERROR_SUCCESS)
77 {
78 GetError();
79 return FALSE;
80 }
81
82 RegQueryValueEx(hKey,
83 _T("Description"),
84 NULL,
85 NULL,
86 (LPBYTE)Description,
87 &Size);
88
89 item.pszText = Description;
90 item.iSubItem = 1;
91 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
92
93
94 /* set the status */
95
96 if (pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
97 {
98 LoadString(hInstance, IDS_SERVICES_STATUS_RUNNING, szStatus, 128);
99 item.pszText = szStatus;
100 item.iSubItem = 2;
101 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
102 }
103 else
104 {
105 item.pszText = '\0';
106 item.iSubItem = 2;
107 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
108 }
109 }
110 }
111
112 return TRUE;
113 }
114
115
116
117
118 DWORD
119 GetServiceList(VOID)
120 {
121 SC_HANDLE ScHandle;
122
123 DWORD BytesNeeded = 0;
124 DWORD ResumeHandle = 0;
125 DWORD NumServices = 0;
126
127 ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
128 if (ScHandle != INVALID_HANDLE_VALUE)
129 {
130 if (EnumServicesStatusEx(ScHandle,
131 SC_ENUM_PROCESS_INFO,
132 SERVICE_WIN32,
133 SERVICE_STATE_ALL,
134 (LPBYTE)pServiceStatus,
135 0, &BytesNeeded,
136 &NumServices,
137 &ResumeHandle,
138 0) == 0)
139 {
140 /* Call function again if required size was returned */
141 if (GetLastError() == ERROR_MORE_DATA)
142 {
143 /* reserve memory for service info array */
144 pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
145 if (pServiceStatus == NULL)
146 return FALSE;
147
148 /* fill array with service info */
149 if (EnumServicesStatusEx(ScHandle,
150 SC_ENUM_PROCESS_INFO,
151 SERVICE_WIN32,
152 SERVICE_STATE_ALL,
153 (LPBYTE)pServiceStatus,
154 BytesNeeded,
155 &BytesNeeded,
156 &NumServices,
157 &ResumeHandle,
158 0) == 0)
159 {
160 HeapFree(GetProcessHeap(), 0, pServiceStatus);
161 return FALSE;
162 }
163 }
164 else /* exit on failure */
165 {
166 return FALSE;
167 }
168 }
169 }
170
171 CloseServiceHandle(ScHandle);
172
173 return NumServices;
174 }
175
176
177
178
179
180
181
182
183
184
185
186 /*
187 //WORD wCodePage;
188 //WORD wLangID;
189 //SC_HANDLE hService;
190 //DWORD dwHandle, dwLen;
191 //UINT BufLen;
192 //TCHAR* lpData;
193 //TCHAR* lpBuffer;
194 //TCHAR szStrFileInfo[80];
195 //TCHAR FileName[MAX_PATH];
196 //LPVOID pvData;
197
198 //LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
199 //LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
200
201 BytesNeeded = 0;
202 hService = OpenService(ScHandle,
203 pServiceStatus[Index].lpServiceName,
204 SC_MANAGER_CONNECT);
205 if (hService != INVALID_HANDLE_VALUE)
206 {
207 / * check if service is required by the system* /
208 if (!QueryServiceConfig2(hService,
209 SERVICE_CONFIG_FAILURE_ACTIONS,
210 (LPBYTE)pServiceFailureActions,
211 0,
212 &BytesNeeded))
213 {
214 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
215 {
216 pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS)
217 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
218 if (pServiceFailureActions == NULL)
219 return FALSE;
220
221 if (!QueryServiceConfig2(hService,
222 SERVICE_CONFIG_FAILURE_ACTIONS,
223 (LPBYTE)pServiceFailureActions,
224 BytesNeeded,
225 &BytesNeeded))
226 {
227 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
228 return FALSE;
229 }
230 }
231 else / * exit on failure * /
232 {
233 return FALSE;
234 }
235 }
236 if (pServiceFailureActions->cActions)
237 {
238 if (pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
239 {
240 LoadString(hInstance, IDS_SERVICES_YES, szStatus, 128);
241 item.pszText = szStatus;
242 item.iSubItem = 1;
243 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
244 }
245 }
246
247 if (pServiceFailureActions != NULL)
248 {
249 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
250 pServiceFailureActions = NULL;
251 }
252
253 / * get vendor of service binary * /
254 BytesNeeded = 0;
255 if (!QueryServiceConfig(hService, pServiceConfig, 0, &BytesNeeded))
256 {
257 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
258 {
259 pServiceConfig = (LPQUERY_SERVICE_CONFIG)
260 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
261 if (pServiceConfig == NULL)
262 return FALSE;
263
264 if (!QueryServiceConfig(hService,
265 pServiceConfig,
266 BytesNeeded,
267 &BytesNeeded))
268 {
269 HeapFree(GetProcessHeap(), 0, pServiceConfig);
270 return FALSE;
271 }
272 }
273 else / * exit on failure * /
274 {
275 return FALSE;
276 }
277 }
278
279 memset(&FileName, 0, MAX_PATH);
280 if (_tcscspn(pServiceConfig->lpBinaryPathName, _T("\"")))
281 {
282 _tcsncpy(FileName, pServiceConfig->lpBinaryPathName,
283 _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")) );
284 }
285 else
286 {
287 _tcscpy(FileName, pServiceConfig->lpBinaryPathName);
288 }
289
290 HeapFree(GetProcessHeap(), 0, pServiceConfig);
291 pServiceConfig = NULL;
292
293 dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
294 if (dwLen)
295 {
296 lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen);
297 if (lpData == NULL)
298 return FALSE;
299
300 if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData)) {
301 HeapFree(GetProcessHeap(), 0, lpData);
302 return FALSE;
303 }
304
305 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
306 {
307 wCodePage = LOWORD(*(DWORD*) pvData);
308 wLangID = HIWORD(*(DWORD*) pvData);
309 wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
310 }
311
312 if (VerQueryValue (lpData, szStrFileInfo, (LPVOID) &lpBuffer, (PUINT) &BufLen)) {
313 item.pszText = lpBuffer;
314 item.iSubItem = 2;
315 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
316 }
317 HeapFree(GetProcessHeap(), 0, lpData);
318 }
319 else
320 {
321 LoadString(hInstance, IDS_SERVICES_UNKNOWN, szStatus, 128);
322 item.pszText = szStatus;
323 item.iSubItem = 2;
324 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
325 }
326 CloseServiceHandle(hService);
327 }
328 */