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