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>
13 HWND hServicesListCtrl
;
16 void GetServices ( void );
19 ServicesPageWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
25 UNREFERENCED_PARAMETER(lParam
);
26 UNREFERENCED_PARAMETER(wParam
);
31 hServicesListCtrl
= GetDlgItem(hDlg
, IDC_SERVICES_LIST
);
32 hServicesDialog
= hDlg
;
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
);
38 SetWindowPos(hDlg
, NULL
, 10, 32, 0, 0, SWP_NOACTIVATE
| SWP_NOOWNERZORDER
| SWP_NOSIZE
| SWP_NOZORDER
);
40 // Initialize the application page's controls
41 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
43 LoadString(hInst
, IDS_SERVICES_COLUMN_SERVICE
, szTemp
, 256);
44 column
.pszText
= szTemp
;
46 (void)ListView_InsertColumn(hServicesListCtrl
, 0, &column
);
48 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
49 LoadString(hInst
, IDS_SERVICES_COLUMN_REQ
, szTemp
, 256);
50 column
.pszText
= szTemp
;
52 (void)ListView_InsertColumn(hServicesListCtrl
, 1, &column
);
54 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
55 LoadString(hInst
, IDS_SERVICES_COLUMN_VENDOR
, szTemp
, 256);
56 column
.pszText
= szTemp
;
58 (void)ListView_InsertColumn(hServicesListCtrl
, 2, &column
);
60 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
61 LoadString(hInst
, IDS_SERVICES_COLUMN_STATUS
, szTemp
, 256);
62 column
.pszText
= szTemp
;
64 (void)ListView_InsertColumn(hServicesListCtrl
, 3, &column
);
81 DWORD BytesNeeded
= 0;
82 DWORD ResumeHandle
= 0;
83 DWORD NumServices
= 0;
84 DWORD dwHandle
, dwLen
;
90 TCHAR szStrFileInfo
[80];
91 TCHAR FileName
[MAX_PATH
];
94 LPSERVICE_FAILURE_ACTIONS pServiceFailureActions
= NULL
;
95 LPQUERY_SERVICE_CONFIG pServiceConfig
= NULL
;
96 ENUM_SERVICE_STATUS_PROCESS
*pServiceStatus
= NULL
;
98 ScHandle
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
99 if (ScHandle
!= INVALID_HANDLE_VALUE
)
101 if (EnumServicesStatusEx(ScHandle
, SC_ENUM_PROCESS_INFO
, SERVICE_WIN32
, SERVICE_STATE_ALL
, (LPBYTE
)pServiceStatus
, 0, &BytesNeeded
, &NumServices
, &ResumeHandle
, 0) == 0)
103 /* Call function again if required size was returned */
104 if (GetLastError() == ERROR_MORE_DATA
)
106 /* reserve memory for service info array */
107 pServiceStatus
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
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)
114 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
118 else /* exit on failure */
128 for (Index
= 0; Index
< NumServices
; Index
++)
130 memset(&item
, 0, sizeof(LV_ITEM
));
131 item
.mask
= LVIF_TEXT
;
133 item
.pszText
= pServiceStatus
[Index
].lpDisplayName
;
134 item
.iItem
= ListView_GetItemCount(hServicesListCtrl
);
136 item
.iItem
= ListView_InsertItem(hServicesListCtrl
, &item
);
138 if (pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_RUNNING
)
140 ListView_SetCheckState(hServicesListCtrl
, item
.iItem
, TRUE
);
144 hService
= OpenService(ScHandle
, pServiceStatus
[Index
].lpServiceName
, SC_MANAGER_CONNECT
);
145 if (hService
!= INVALID_HANDLE_VALUE
)
147 /* check if service is required by the system*/
148 if (!QueryServiceConfig2(hService
, SERVICE_CONFIG_FAILURE_ACTIONS
, (LPBYTE
)NULL
, 0, &BytesNeeded
))
150 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
152 pServiceFailureActions
= (LPSERVICE_FAILURE_ACTIONS
) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
153 if (pServiceFailureActions
== NULL
)
156 if (!QueryServiceConfig2(hService
, SERVICE_CONFIG_FAILURE_ACTIONS
, (LPBYTE
)pServiceFailureActions
, BytesNeeded
, &BytesNeeded
))
158 HeapFree(GetProcessHeap(), 0, pServiceFailureActions
);
159 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
160 CloseServiceHandle(hService
);
161 CloseServiceHandle(ScHandle
);
165 else /* exit on failure */
167 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
168 CloseServiceHandle(hService
);
169 CloseServiceHandle(ScHandle
);
173 if (pServiceFailureActions
->cActions
)
175 if (pServiceFailureActions
->lpsaActions
[0].Type
== SC_ACTION_REBOOT
)
177 LoadString(hInst
, IDS_SERVICES_YES
, szStatus
, 128);
178 item
.pszText
= szStatus
;
180 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
184 if (pServiceFailureActions
!= NULL
)
186 HeapFree(GetProcessHeap(), 0, pServiceFailureActions
);
187 pServiceFailureActions
= NULL
;
190 /* get vendor of service binary */
192 if (!QueryServiceConfig(hService
, NULL
, 0, &BytesNeeded
))
194 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
196 pServiceConfig
= (LPQUERY_SERVICE_CONFIG
) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
197 if (pServiceConfig
== NULL
)
199 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
200 CloseServiceHandle(hService
);
201 CloseServiceHandle(ScHandle
);
204 if (!QueryServiceConfig(hService
, pServiceConfig
, BytesNeeded
, &BytesNeeded
))
206 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
207 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
208 CloseServiceHandle(hService
);
209 CloseServiceHandle(ScHandle
);
213 else /* exit on failure */
215 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
216 CloseServiceHandle(hService
);
217 CloseServiceHandle(ScHandle
);
222 memset(&FileName
, 0, MAX_PATH
);
223 if (_tcscspn(pServiceConfig
->lpBinaryPathName
, _T("\"")))
225 _tcsncpy(FileName
, pServiceConfig
->lpBinaryPathName
, _tcscspn(pServiceConfig
->lpBinaryPathName
, _T(" ")) );
229 _tcscpy(FileName
, pServiceConfig
->lpBinaryPathName
);
232 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
233 pServiceConfig
= NULL
;
235 dwLen
= GetFileVersionInfoSize(FileName
, &dwHandle
);
238 lpData
= (TCHAR
*) HeapAlloc(GetProcessHeap(), 0, dwLen
);
241 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
242 CloseServiceHandle(hService
);
243 CloseServiceHandle(ScHandle
);
246 if (!GetFileVersionInfo (FileName
, dwHandle
, dwLen
, lpData
))
248 HeapFree(GetProcessHeap(), 0, lpData
);
249 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
250 CloseServiceHandle(hService
);
251 CloseServiceHandle(ScHandle
);
255 if (VerQueryValue(lpData
, _T("\\VarFileInfo\\Translation"), &pvData
, (PUINT
) &BufLen
))
257 wCodePage
= LOWORD(*(DWORD
*) pvData
);
258 wLangID
= HIWORD(*(DWORD
*) pvData
);
259 wsprintf(szStrFileInfo
, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage
, wLangID
);
262 if (VerQueryValue (lpData
, szStrFileInfo
, (void**) &lpBuffer
, (PUINT
) &BufLen
))
264 item
.pszText
= lpBuffer
;
266 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
268 HeapFree(GetProcessHeap(), 0, lpData
);
272 LoadString(hInst
, IDS_SERVICES_UNKNOWN
, szStatus
, 128);
273 item
.pszText
= szStatus
;
275 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
277 CloseServiceHandle(hService
);
280 LoadString(hInst
, ((pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_STOPPED
) ? IDS_SERVICES_STATUS_STOPPED
: IDS_SERVICES_STATUS_RUNNING
), szStatus
, 128);
281 item
.pszText
= szStatus
;
283 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
288 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
289 CloseServiceHandle(ScHandle
);