ReactOS 0.4.15-dev-5667-ged97270
srvpage.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/msconfig/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
18
19void GetServices ( void );
20
23{
25 TCHAR szTemp[256];
26 DWORD dwStyle;
27
30
31 switch (message) {
32 case WM_INITDIALOG:
33
35 hServicesDialog = hDlg;
36
38 dwStyle = dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES;
40
42
43 // Initialize the application page's controls
45
47 column.pszText = szTemp;
48 column.cx = 200;
50
53 column.pszText = szTemp;
54 column.cx = 70;
56
59 column.pszText = szTemp;
60 column.cx = 200;
62
65 column.pszText = szTemp;
66 column.cx = 70;
68
70 return TRUE;
71 }
72
73 return 0;
74}
75
76void
78{
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;
90 TCHAR szStatus[128];
91 TCHAR* lpData;
93 TCHAR szStrFileInfo[80];
96
97 LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
98 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
99 ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
100
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 */
108 {
109 /* reserve memory for service info array */
110 pServiceStatus = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
111 if (!pServiceStatus)
112 {
113 CloseServiceHandle(ScHandle);
114 return;
115 }
116
117 /* fill array with service info */
118 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BytesNeeded, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
119 {
120 HeapFree(GetProcessHeap(), 0, pServiceStatus);
121 CloseServiceHandle(ScHandle);
122 return;
123 }
124 }
125 else /* exit on failure */
126 {
127 CloseServiceHandle(ScHandle);
128 return;
129 }
130 }
131
132 if (NumServices)
133 {
134 if (!pServiceStatus)
135 {
136 CloseServiceHandle(ScHandle);
137 return;
138 }
139
140 for (Index = 0; Index < NumServices; Index++)
141 {
142 memset(&item, 0, sizeof(LV_ITEM));
143 item.mask = LVIF_TEXT;
144 item.iImage = 0;
145 item.pszText = pServiceStatus[Index].lpDisplayName;
147 item.lParam = 0;
149
150 if (pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
151 {
153 }
154
155 BytesNeeded = 0;
156 hService = OpenService(ScHandle, pServiceStatus[Index].lpServiceName, SC_MANAGER_CONNECT);
157 if (hService != NULL)
158 {
159 /* check if service is required by the system*/
160 if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)NULL, 0, &BytesNeeded))
161 {
163 {
164 pServiceFailureActions = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
165 if (pServiceFailureActions == NULL)
166 {
167 HeapFree(GetProcessHeap(), 0, pServiceStatus);
168 CloseServiceHandle(hService);
169 CloseServiceHandle(ScHandle);
170 return;
171 }
172
173 if (!QueryServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE)pServiceFailureActions, BytesNeeded, &BytesNeeded))
174 {
175 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
176 HeapFree(GetProcessHeap(), 0, pServiceStatus);
177 CloseServiceHandle(hService);
178 CloseServiceHandle(ScHandle);
179 return;
180 }
181 }
182 else /* exit on failure */
183 {
184 HeapFree(GetProcessHeap(), 0, pServiceStatus);
185 CloseServiceHandle(hService);
186 CloseServiceHandle(ScHandle);
187 return;
188 }
189 }
190
191 if (pServiceFailureActions != NULL)
192 {
193 if (pServiceFailureActions->cActions && pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
194 {
195 LoadString(hInst, IDS_SERVICES_YES, szStatus, 128);
196 item.pszText = szStatus;
197 item.iSubItem = 1;
199 }
200 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
201 pServiceFailureActions = NULL;
202 }
203
204 /* get vendor of service binary */
205 BytesNeeded = 0;
206 if (!QueryServiceConfig(hService, NULL, 0, &BytesNeeded))
207 {
209 {
210 pServiceConfig = HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
211 if (pServiceConfig == NULL)
212 {
213 HeapFree(GetProcessHeap(), 0, pServiceStatus);
214 CloseServiceHandle(hService);
215 CloseServiceHandle(ScHandle);
216 return;
217 }
218 if (!QueryServiceConfig(hService, pServiceConfig, BytesNeeded, &BytesNeeded))
219 {
220 HeapFree(GetProcessHeap(), 0, pServiceConfig);
221 HeapFree(GetProcessHeap(), 0, pServiceStatus);
222 CloseServiceHandle(hService);
223 CloseServiceHandle(ScHandle);
224 return;
225 }
226 }
227 else /* exit on failure */
228 {
229 HeapFree(GetProcessHeap(), 0, pServiceStatus);
230 CloseServiceHandle(hService);
231 CloseServiceHandle(ScHandle);
232 return;
233 }
234 }
235
236 if (pServiceConfig->lpBinaryPathName[0] != _T('"'))
237 {
238 /* Assume everything before the first space is the binary path */
239 /* FIXME: This is a reasonable heuristic but some
240 * services use unquoted paths with spaces */
243 pServiceConfig->lpBinaryPathName,
244 _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")));
245 }
246 else
247 {
248 /* Binary path is inside the quotes */
251 pServiceConfig->lpBinaryPathName + 1,
252 _tcscspn(pServiceConfig->lpBinaryPathName + 1, _T("\"")));
253 }
254
255 HeapFree(GetProcessHeap(), 0, pServiceConfig);
256 pServiceConfig = NULL;
257
258 dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
259 if (dwLen)
260 {
261 lpData = HeapAlloc(GetProcessHeap(), 0, dwLen);
262 if (lpData == NULL)
263 {
264 HeapFree(GetProcessHeap(), 0, pServiceStatus);
265 CloseServiceHandle(hService);
266 CloseServiceHandle(ScHandle);
267 return;
268 }
269 if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData))
270 {
271 HeapFree(GetProcessHeap(), 0, lpData);
272 HeapFree(GetProcessHeap(), 0, pServiceStatus);
273 CloseServiceHandle(hService);
274 CloseServiceHandle(ScHandle);
275 return;
276 }
277
278 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
279 {
280 wCodePage = LOWORD(*(DWORD*) pvData);
281 wLangID = HIWORD(*(DWORD*) pvData);
282 wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
283 }
284
285 if (VerQueryValue (lpData, szStrFileInfo, (void**) &lpBuffer, (PUINT) &BufLen))
286 {
287 item.pszText = lpBuffer;
288 item.iSubItem = 2;
290 }
291 HeapFree(GetProcessHeap(), 0, lpData);
292 }
293 else
294 {
295 LoadString(hInst, IDS_SERVICES_UNKNOWN, szStatus, 128);
296 item.pszText = szStatus;
297 item.iSubItem = 2;
299 }
300 CloseServiceHandle(hService);
301 }
302
303 LoadString(hInst, ((pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_STOPPED) ? IDS_SERVICES_STATUS_STOPPED : IDS_SERVICES_STATUS_RUNNING), szStatus, 128);
304 item.pszText = szStatus;
305 item.iSubItem = 3;
307
308 }
309 }
310
311 HeapFree(GetProcessHeap(), 0, pServiceStatus);
312 CloseServiceHandle(ScHandle);
313 }
314
315}
#define IDS_SERVICES_YES
Definition: resource.h:111
#define IDS_SERVICES_UNKNOWN
Definition: resource.h:112
#define IDS_SERVICES_STATUS_STOPPED
Definition: resource.h:109
#define IDS_SERVICES_COLUMN_VENDOR
Definition: resource.h:83
#define IDS_SERVICES_COLUMN_SERVICE
Definition: resource.h:81
#define IDS_SERVICES_COLUMN_REQ
Definition: resource.h:82
#define IDC_SERVICES_LIST
Definition: resource.h:17
#define IDS_SERVICES_STATUS_RUNNING
Definition: resource.h:110
#define IDS_SERVICES_COLUMN_STATUS
Definition: resource.h:84
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define CALLBACK
Definition: compat.h:35
HINSTANCE hInst
Definition: dxdiag.c:13
#define BufLen
Definition: fatfs.h:167
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
#define _tcscspn
Definition: tchar.h:1407
static ATOM item
Definition: dde.c:856
unsigned int * PUINT
Definition: ndis.h:50
unsigned int UINT
Definition: ndis.h:50
#define DWORD
Definition: nt_native.h:44
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define LOWORD(l)
Definition: pedump.c:82
#define LVM_SETITEMTEXT
Definition: commctrl.h:2689
#define ListView_InsertItem(hwnd, pitem)
Definition: commctrl.h:2408
#define ListView_InsertColumn(hwnd, iCol, pcol)
Definition: commctrl.h:2636
#define ListView_SetCheckState(hwndLV, i, fCheck)
Definition: commctrl.h:2674
#define LVCF_WIDTH
Definition: commctrl.h:2587
#define LVS_EX_CHECKBOXES
Definition: commctrl.h:2731
#define LVS_EX_FULLROWSELECT
Definition: commctrl.h:2734
#define ListView_GetItemCount(hwnd)
Definition: commctrl.h:2307
#define LV_ITEM
Definition: commctrl.h:2337
#define LVIF_TEXT
Definition: commctrl.h:2309
#define LVM_GETEXTENDEDLISTVIEWSTYLE
Definition: commctrl.h:2727
#define LVCF_TEXT
Definition: commctrl.h:2588
#define LVM_SETEXTENDEDLISTVIEWSTYLE
Definition: commctrl.h:2724
#define LV_COLUMN
Definition: commctrl.h:2547
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
#define memset(x, y, z)
Definition: compat.h:39
#define _countof(array)
Definition: sndvol32.h:68
HWND hServicesListCtrl
Definition: srvpage.c:16
HWND hServicesDialog
Definition: srvpage.c:17
HWND hServicesPage
Definition: srvpage.c:15
void GetServices(void)
Definition: srvpage.c:77
INT_PTR CALLBACK ServicesPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: srvpage.c:22
#define StringCchCopyN
Definition: strsafe.h:227
SC_ACTION_TYPE Type
Definition: winsvc.h:205
SC_ACTION * lpsaActions
Definition: winsvc.h:213
Definition: tftpd.h:60
int32_t INT_PTR
Definition: typedefs.h:64
unsigned char * LPBYTE
Definition: typedefs.h:53
#define HIWORD(l)
Definition: typedefs.h:247
#define _T(x)
Definition: vfdio.h:22
_In_ WDFCOLLECTION _In_ ULONG Index
DWORD WINAPI GetLastError(void)
Definition: except.c:1040
_In_ ULONG _In_opt_ PVOID pvData
Definition: winddi.h:3749
LONG_PTR LPARAM
Definition: windef.h:208
UINT_PTR WPARAM
Definition: windef.h:207
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_STATE_ALL
Definition: winsvc.h:52
#define QueryServiceConfig
Definition: winsvc.h:580
#define OpenSCManager
Definition: winsvc.h:575
@ SC_ACTION_REBOOT
Definition: winsvc.h:201
#define SC_MANAGER_ENUMERATE_SERVICE
Definition: winsvc.h:16
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define EnumServicesStatusEx
Definition: winsvc.h:572
#define SERVICE_CONFIG_FAILURE_ACTIONS
Definition: winsvc.h:66
@ SC_ENUM_PROCESS_INFO
Definition: winsvc.h:122
#define QueryServiceConfig2
Definition: winsvc.h:581
#define OpenService
Definition: winsvc.h:576
#define SWP_NOACTIVATE
Definition: winuser.h:1232
BOOL WINAPI SetWindowPos(_In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
#define SWP_NOSIZE
Definition: winuser.h:1235
#define WM_INITDIALOG
Definition: winuser.h:1729
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define SendMessage
Definition: winuser.h:5833
#define wsprintf
Definition: winuser.h:5855
#define LoadString
Definition: winuser.h:5809
#define SWP_NOOWNERZORDER
Definition: winuser.h:1239
#define SWP_NOZORDER
Definition: winuser.h:1237
#define VerQueryValue
Definition: winver.h:56
#define GetFileVersionInfo
Definition: winver.h:54
#define GetFileVersionInfoSize
Definition: winver.h:53
#define SERVICE_WIN32
Definition: cmtypes.h:964
char TCHAR
Definition: xmlstorage.h:189