88b1d74be2c817eac94cab059d02a1e8d5618e6f
[reactos.git] / reactos / base / applications / msconfig / srvpage.c
1 /*
2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/srvpage.c
5 * PURPOSE: Services page message handler
6 * COPYRIGHT: Copyright 2005-2006 Christoph von Wittich <Christoph@ApiViewer.de>
7 *
8 */
9
10 #include "precomp.h"
11
12 #include <winsvc.h>
13 #include <winver.h>
14
15 HWND hServicesPage;
16 HWND hServicesListCtrl;
17 HWND hServicesDialog;
18
19 void GetServices ( void );
20
21 INT_PTR CALLBACK
22 ServicesPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
23 {
24 LV_COLUMN column;
25 TCHAR szTemp[256];
26 DWORD dwStyle;
27
28 UNREFERENCED_PARAMETER(lParam);
29 UNREFERENCED_PARAMETER(wParam);
30
31 switch (message) {
32 case WM_INITDIALOG:
33
34 hServicesListCtrl = GetDlgItem(hDlg, IDC_SERVICES_LIST);
35 hServicesDialog = hDlg;
36
37 dwStyle = (DWORD) SendMessage(hServicesListCtrl, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
38 dwStyle = dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES;
39 SendMessage(hServicesListCtrl, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, dwStyle);
40
41 SetWindowPos(hDlg, NULL, 10, 32, 0, 0, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
42
43 // Initialize the application page's controls
44 column.mask = LVCF_TEXT | LVCF_WIDTH;
45
46 LoadString(hInst, IDS_SERVICES_COLUMN_SERVICE, szTemp, 256);
47 column.pszText = szTemp;
48 column.cx = 200;
49 (void)ListView_InsertColumn(hServicesListCtrl, 0, &column);
50
51 column.mask = LVCF_TEXT | LVCF_WIDTH;
52 LoadString(hInst, IDS_SERVICES_COLUMN_REQ, szTemp, 256);
53 column.pszText = szTemp;
54 column.cx = 70;
55 (void)ListView_InsertColumn(hServicesListCtrl, 1, &column);
56
57 column.mask = LVCF_TEXT | LVCF_WIDTH;
58 LoadString(hInst, IDS_SERVICES_COLUMN_VENDOR, szTemp, 256);
59 column.pszText = szTemp;
60 column.cx = 200;
61 (void)ListView_InsertColumn(hServicesListCtrl, 2, &column);
62
63 column.mask = LVCF_TEXT | LVCF_WIDTH;
64 LoadString(hInst, IDS_SERVICES_COLUMN_STATUS, szTemp, 256);
65 column.pszText = szTemp;
66 column.cx = 70;
67 (void)ListView_InsertColumn(hServicesListCtrl, 3, &column);
68
69 GetServices();
70 return TRUE;
71 }
72
73 return 0;
74 }
75
76 void
77 GetServices ( void )
78 {
79 LV_ITEM item;
80 WORD wCodePage;
81 WORD wLangID;
82 SC_HANDLE ScHandle;
83 SC_HANDLE hService;
84 DWORD BytesNeeded = 0;
85 DWORD ResumeHandle = 0;
86 DWORD NumServices = 0;
87 DWORD dwHandle, dwLen;
88 size_t Index;
89 UINT BufLen;
90 TCHAR szStatus[128];
91 TCHAR* lpData;
92 TCHAR* lpBuffer;
93 TCHAR szStrFileInfo[80];
94 TCHAR FileName[MAX_PATH];
95 LPVOID pvData;
96
97 LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
98 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
99 ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
100
101 ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
102 if (ScHandle != NULL)
103 {
104 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, 0, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
105 {
106 /* Call function again if required size was returned */
107 if (GetLastError() == ERROR_MORE_DATA)
108 {
109 /* reserve memory for service info array */
110 pServiceStatus = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
111 if (!pServiceStatus)
112 return;
113
114 /* fill array with service info */
115 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BytesNeeded, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
116 {
117 HeapFree(GetProcessHeap(), 0, pServiceStatus);
118 return;
119 }
120 }
121 else /* exit on failure */
122 {
123 return;
124 }
125 }
126
127 if (NumServices)
128 {
129 if (!pServiceStatus)
130 return;
131 for (Index = 0; Index < NumServices; Index++)
132 {
133 memset(&item, 0, sizeof(LV_ITEM));
134 item.mask = LVIF_TEXT;
135 item.iImage = 0;
136 item.pszText = pServiceStatus[Index].lpDisplayName;
137 item.iItem = ListView_GetItemCount(hServicesListCtrl);
138 item.lParam = 0;
139 item.iItem = ListView_InsertItem(hServicesListCtrl, &item);
140
141 if (pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
142 {
143 ListView_SetCheckState(hServicesListCtrl, item.iItem, TRUE);
144 }
145
146 BytesNeeded = 0;
147 hService = OpenService(ScHandle, pServiceStatus[Index].lpServiceName, SC_MANAGER_CONNECT);
148 if (hService != NULL)
149 {
150 /* check if service is required by the system*/
151 if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded))
152 {
153 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
154 {
155 pServiceFailureActions = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
156 if (pServiceFailureActions == NULL)
157 {
158 HeapFree(GetProcessHeap(), 0, pServiceStatus);
159 CloseServiceHandle(hService);
160 CloseServiceHandle(ScHandle);
161 return;
162 }
163
164 if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded))
165 {
166 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
167 HeapFree(GetProcessHeap(), 0, pServiceStatus);
168 CloseServiceHandle(hService);
169 CloseServiceHandle(ScHandle);
170 return;
171 }
172 }
173 else /* exit on failure */
174 {
175 HeapFree(GetProcessHeap(), 0, pServiceStatus);
176 CloseServiceHandle(hService);
177 CloseServiceHandle(ScHandle);
178 return;
179 }
180 }
181 if (pServiceFailureActions->cActions)
182 {
183 if (pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
184 {
185 LoadString(hInst, IDS_SERVICES_YES, szStatus, 128);
186 item.pszText = szStatus;
187 item.iSubItem = 1;
188 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
189 }
190 }
191
192 if (pServiceFailureActions != NULL)
193 {
194 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
195 pServiceFailureActions = NULL;
196 }
197
198 /* get vendor of service binary */
199 BytesNeeded = 0;
200 if (!QueryServiceConfig(hService, NULL, 0, &BytesNeeded))
201 {
202 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
203 {
204 pServiceConfig = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
205 if (pServiceConfig == NULL)
206 {
207 HeapFree(GetProcessHeap(), 0, pServiceStatus);
208 CloseServiceHandle(hService);
209 CloseServiceHandle(ScHandle);
210 return;
211 }
212 if (!QueryServiceConfig(hService, pServiceConfig, BytesNeeded, &BytesNeeded))
213 {
214 HeapFree(GetProcessHeap(), 0, pServiceConfig);
215 HeapFree(GetProcessHeap(), 0, pServiceStatus);
216 CloseServiceHandle(hService);
217 CloseServiceHandle(ScHandle);
218 return;
219 }
220 }
221 else /* exit on failure */
222 {
223 HeapFree(GetProcessHeap(), 0, pServiceStatus);
224 CloseServiceHandle(hService);
225 CloseServiceHandle(ScHandle);
226 return;
227 }
228 }
229
230 memset(&FileName, 0, MAX_PATH);
231 if (_tcscspn(pServiceConfig->lpBinaryPathName, _T("\"")))
232 {
233 _tcsncpy(FileName, pServiceConfig->lpBinaryPathName, _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")) );
234 }
235 else
236 {
237 _tcscpy(FileName, pServiceConfig->lpBinaryPathName);
238 }
239
240 HeapFree(GetProcessHeap(), 0, pServiceConfig);
241 pServiceConfig = NULL;
242
243 dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
244 if (dwLen)
245 {
246 lpData = HeapAlloc(GetProcessHeap(), 0, dwLen);
247 if (lpData == NULL)
248 {
249 HeapFree(GetProcessHeap(), 0, pServiceStatus);
250 CloseServiceHandle(hService);
251 CloseServiceHandle(ScHandle);
252 return;
253 }
254 if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData))
255 {
256 HeapFree(GetProcessHeap(), 0, lpData);
257 HeapFree(GetProcessHeap(), 0, pServiceStatus);
258 CloseServiceHandle(hService);
259 CloseServiceHandle(ScHandle);
260 return;
261 }
262
263 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
264 {
265 wCodePage = LOWORD(*(DWORD*) pvData);
266 wLangID = HIWORD(*(DWORD*) pvData);
267 wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
268 }
269
270 if (VerQueryValue (lpData, szStrFileInfo, (void**) &lpBuffer, (PUINT) &BufLen))
271 {
272 item.pszText = lpBuffer;
273 item.iSubItem = 2;
274 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
275 }
276 HeapFree(GetProcessHeap(), 0, lpData);
277 }
278 else
279 {
280 LoadString(hInst, IDS_SERVICES_UNKNOWN, szStatus, 128);
281 item.pszText = szStatus;
282 item.iSubItem = 2;
283 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
284 }
285 CloseServiceHandle(hService);
286 }
287
288 LoadString(hInst, ((pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_STOPPED) ? IDS_SERVICES_STATUS_STOPPED : IDS_SERVICES_STATUS_RUNNING), szStatus, 128);
289 item.pszText = szStatus;
290 item.iSubItem = 3;
291 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
292
293 }
294 }
295
296 HeapFree(GetProcessHeap(), 0, pServiceStatus);
297 CloseServiceHandle(ScHandle);
298 }
299
300 }