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>
16 HWND hServicesListCtrl
;
19 void GetServices ( void );
22 ServicesPageWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
28 UNREFERENCED_PARAMETER(lParam
);
29 UNREFERENCED_PARAMETER(wParam
);
34 hServicesListCtrl
= GetDlgItem(hDlg
, IDC_SERVICES_LIST
);
35 hServicesDialog
= hDlg
;
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
);
41 SetWindowPos(hDlg
, NULL
, 10, 32, 0, 0, SWP_NOACTIVATE
| SWP_NOOWNERZORDER
| SWP_NOSIZE
| SWP_NOZORDER
);
43 // Initialize the application page's controls
44 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
46 LoadString(hInst
, IDS_SERVICES_COLUMN_SERVICE
, szTemp
, 256);
47 column
.pszText
= szTemp
;
49 (void)ListView_InsertColumn(hServicesListCtrl
, 0, &column
);
51 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
52 LoadString(hInst
, IDS_SERVICES_COLUMN_REQ
, szTemp
, 256);
53 column
.pszText
= szTemp
;
55 (void)ListView_InsertColumn(hServicesListCtrl
, 1, &column
);
57 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
58 LoadString(hInst
, IDS_SERVICES_COLUMN_VENDOR
, szTemp
, 256);
59 column
.pszText
= szTemp
;
61 (void)ListView_InsertColumn(hServicesListCtrl
, 2, &column
);
63 column
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
64 LoadString(hInst
, IDS_SERVICES_COLUMN_STATUS
, szTemp
, 256);
65 column
.pszText
= szTemp
;
67 (void)ListView_InsertColumn(hServicesListCtrl
, 3, &column
);
84 DWORD BytesNeeded
= 0;
85 DWORD ResumeHandle
= 0;
86 DWORD NumServices
= 0;
87 DWORD dwHandle
, dwLen
;
93 TCHAR szStrFileInfo
[80];
94 TCHAR FileName
[MAX_PATH
];
97 LPSERVICE_FAILURE_ACTIONS pServiceFailureActions
= NULL
;
98 LPQUERY_SERVICE_CONFIG pServiceConfig
= NULL
;
99 ENUM_SERVICE_STATUS_PROCESS
*pServiceStatus
= NULL
;
101 ScHandle
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
102 if (ScHandle
!= NULL
)
104 if (EnumServicesStatusEx(ScHandle
, SC_ENUM_PROCESS_INFO
, SERVICE_WIN32
, SERVICE_STATE_ALL
, (LPBYTE
)pServiceStatus
, 0, &BytesNeeded
, &NumServices
, &ResumeHandle
, 0) == 0)
106 /* Call function again if required size was returned */
107 if (GetLastError() == ERROR_MORE_DATA
)
109 /* reserve memory for service info array */
110 pServiceStatus
= HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
113 CloseServiceHandle(ScHandle
);
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)
120 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
121 CloseServiceHandle(ScHandle
);
125 else /* exit on failure */
127 CloseServiceHandle(ScHandle
);
136 CloseServiceHandle(ScHandle
);
140 for (Index
= 0; Index
< NumServices
; Index
++)
142 memset(&item
, 0, sizeof(LV_ITEM
));
143 item
.mask
= LVIF_TEXT
;
145 item
.pszText
= pServiceStatus
[Index
].lpDisplayName
;
146 item
.iItem
= ListView_GetItemCount(hServicesListCtrl
);
148 item
.iItem
= ListView_InsertItem(hServicesListCtrl
, &item
);
150 if (pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_RUNNING
)
152 ListView_SetCheckState(hServicesListCtrl
, item
.iItem
, TRUE
);
156 hService
= OpenService(ScHandle
, pServiceStatus
[Index
].lpServiceName
, SC_MANAGER_CONNECT
);
157 if (hService
!= NULL
)
159 /* check if service is required by the system*/
160 if (!QueryServiceConfig2(hService
, SERVICE_CONFIG_FAILURE_ACTIONS
, (LPBYTE
)NULL
, 0, &BytesNeeded
))
162 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
164 pServiceFailureActions
= HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
165 if (pServiceFailureActions
== NULL
)
167 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
168 CloseServiceHandle(hService
);
169 CloseServiceHandle(ScHandle
);
173 if (!QueryServiceConfig2(hService
, SERVICE_CONFIG_FAILURE_ACTIONS
, (LPBYTE
)pServiceFailureActions
, BytesNeeded
, &BytesNeeded
))
175 HeapFree(GetProcessHeap(), 0, pServiceFailureActions
);
176 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
177 CloseServiceHandle(hService
);
178 CloseServiceHandle(ScHandle
);
182 else /* exit on failure */
184 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
185 CloseServiceHandle(hService
);
186 CloseServiceHandle(ScHandle
);
191 if (pServiceFailureActions
!= NULL
)
193 if (pServiceFailureActions
->cActions
&& pServiceFailureActions
->lpsaActions
[0].Type
== SC_ACTION_REBOOT
)
195 LoadString(hInst
, IDS_SERVICES_YES
, szStatus
, 128);
196 item
.pszText
= szStatus
;
198 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
200 HeapFree(GetProcessHeap(), 0, pServiceFailureActions
);
201 pServiceFailureActions
= NULL
;
204 /* get vendor of service binary */
206 if (!QueryServiceConfig(hService
, NULL
, 0, &BytesNeeded
))
208 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
210 pServiceConfig
= HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
211 if (pServiceConfig
== NULL
)
213 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
214 CloseServiceHandle(hService
);
215 CloseServiceHandle(ScHandle
);
218 if (!QueryServiceConfig(hService
, pServiceConfig
, BytesNeeded
, &BytesNeeded
))
220 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
221 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
222 CloseServiceHandle(hService
);
223 CloseServiceHandle(ScHandle
);
227 else /* exit on failure */
229 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
230 CloseServiceHandle(hService
);
231 CloseServiceHandle(ScHandle
);
236 memset(&FileName
, 0, MAX_PATH
);
237 if (_tcscspn(pServiceConfig
->lpBinaryPathName
, _T("\"")))
239 _tcsncpy(FileName
, pServiceConfig
->lpBinaryPathName
, _tcscspn(pServiceConfig
->lpBinaryPathName
, _T(" ")) );
243 _tcscpy(FileName
, pServiceConfig
->lpBinaryPathName
);
246 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
247 pServiceConfig
= NULL
;
249 dwLen
= GetFileVersionInfoSize(FileName
, &dwHandle
);
252 lpData
= HeapAlloc(GetProcessHeap(), 0, dwLen
);
255 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
256 CloseServiceHandle(hService
);
257 CloseServiceHandle(ScHandle
);
260 if (!GetFileVersionInfo (FileName
, dwHandle
, dwLen
, lpData
))
262 HeapFree(GetProcessHeap(), 0, lpData
);
263 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
264 CloseServiceHandle(hService
);
265 CloseServiceHandle(ScHandle
);
269 if (VerQueryValue(lpData
, _T("\\VarFileInfo\\Translation"), &pvData
, (PUINT
) &BufLen
))
271 wCodePage
= LOWORD(*(DWORD
*) pvData
);
272 wLangID
= HIWORD(*(DWORD
*) pvData
);
273 wsprintf(szStrFileInfo
, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage
, wLangID
);
276 if (VerQueryValue (lpData
, szStrFileInfo
, (void**) &lpBuffer
, (PUINT
) &BufLen
))
278 item
.pszText
= lpBuffer
;
280 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
282 HeapFree(GetProcessHeap(), 0, lpData
);
286 LoadString(hInst
, IDS_SERVICES_UNKNOWN
, szStatus
, 128);
287 item
.pszText
= szStatus
;
289 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
291 CloseServiceHandle(hService
);
294 LoadString(hInst
, ((pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_STOPPED
) ? IDS_SERVICES_STATUS_STOPPED
: IDS_SERVICES_STATUS_RUNNING
), szStatus
, 128);
295 item
.pszText
= szStatus
;
297 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
302 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
303 CloseServiceHandle(ScHandle
);