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