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
)pServiceFailureActions
, 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
);
162 else /* exit on failure */
167 if (pServiceFailureActions
->cActions
)
169 if (pServiceFailureActions
->lpsaActions
[0].Type
== SC_ACTION_REBOOT
)
171 LoadString(hInst
, IDS_SERVICES_YES
, szStatus
, 128);
172 item
.pszText
= szStatus
;
174 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
178 if (pServiceFailureActions
!= NULL
)
180 HeapFree(GetProcessHeap(), 0, pServiceFailureActions
);
181 pServiceFailureActions
= NULL
;
184 /* get vendor of service binary */
186 if (!QueryServiceConfig(hService
, pServiceConfig
, 0, &BytesNeeded
))
188 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
190 pServiceConfig
= (LPQUERY_SERVICE_CONFIG
) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
191 if (pServiceConfig
== NULL
)
194 if (!QueryServiceConfig(hService
, pServiceConfig
, BytesNeeded
, &BytesNeeded
))
196 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
200 else /* exit on failure */
206 memset(&FileName
, 0, MAX_PATH
);
207 if (_tcscspn(pServiceConfig
->lpBinaryPathName
, _T("\"")))
209 _tcsncpy(FileName
, pServiceConfig
->lpBinaryPathName
, _tcscspn(pServiceConfig
->lpBinaryPathName
, _T(" ")) );
213 _tcscpy(FileName
, pServiceConfig
->lpBinaryPathName
);
216 HeapFree(GetProcessHeap(), 0, pServiceConfig
);
217 pServiceConfig
= NULL
;
219 dwLen
= GetFileVersionInfoSize(FileName
, &dwHandle
);
222 lpData
= (TCHAR
*) HeapAlloc(GetProcessHeap(), 0, dwLen
);
226 if (!GetFileVersionInfo (FileName
, dwHandle
, dwLen
, lpData
))
228 HeapFree(GetProcessHeap(), 0, lpData
);
232 if (VerQueryValue(lpData
, _T("\\VarFileInfo\\Translation"), &pvData
, (PUINT
) &BufLen
))
234 wCodePage
= LOWORD(*(DWORD
*) pvData
);
235 wLangID
= HIWORD(*(DWORD
*) pvData
);
236 wsprintf(szStrFileInfo
, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage
, wLangID
);
239 if (VerQueryValue (lpData
, szStrFileInfo
, (void**) &lpBuffer
, (PUINT
) &BufLen
))
241 item
.pszText
= lpBuffer
;
243 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
245 HeapFree(GetProcessHeap(), 0, lpData
);
249 LoadString(hInst
, IDS_SERVICES_UNKNOWN
, szStatus
, 128);
250 item
.pszText
= szStatus
;
252 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
254 CloseServiceHandle(hService
);
257 LoadString(hInst
, ((pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_STOPPED
) ? IDS_SERVICES_STATUS_STOPPED
: IDS_SERVICES_STATUS_RUNNING
), szStatus
, 128);
258 item
.pszText
= szStatus
;
260 SendMessage(hServicesListCtrl
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
265 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
266 CloseServiceHandle(ScHandle
);