list services with EnumServicesStatusEx instead of reading them from registry
[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 = 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 SC_HANDLE ScHandle;
66 DWORD BufSize = 0;
67 DWORD BytesNeeded = 0;
68 DWORD ResumeHandle = 0;
69 DWORD NumServices = 0;
70 size_t Index;
71 TCHAR szStatus[128];
72
73 ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
74
75 ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
76 if (ScHandle != INVALID_HANDLE_VALUE)
77 {
78 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BufSize, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
79 {
80 /* Call function again if required size was returned */
81 if (GetLastError() == ERROR_MORE_DATA)
82 {
83 /* reserve memory for service info array */
84 pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
85 if (pServiceStatus == NULL)
86 return;
87
88 /* fill array with service info */
89 if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BytesNeeded, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
90 {
91 HeapFree(GetProcessHeap(), 0, pServiceStatus);
92 return;
93 }
94 }
95 else /* exit on failure */
96 {
97 return;
98 }
99 }
100
101 if (NumServices)
102 {
103 for (Index = 0; Index < NumServices; Index++)
104 {
105 memset(&item, 0, sizeof(LV_ITEM));
106 item.mask = LVIF_TEXT;
107 item.iImage = 0;
108 item.pszText = pServiceStatus[Index].lpDisplayName;
109 item.iItem = ListView_GetItemCount(hServicesListCtrl);
110 item.lParam = 0;
111 item.iItem = ListView_InsertItem(hServicesListCtrl, &item);
112
113 /* FIXME
114 if (QueryServiceConfig2(ScHandle, SERVICE_CONFIG_FAILURE_ACTIONS, ) == 0)
115 {
116 if (GetLastError() == ERROR_MORE_DATA)
117 {
118
119 }
120 }
121 LoadString(hInst, ( CONDITION ? IDS_YES : IDS_NO), szStatus, 128);
122 item.pszText = szStatus;
123 item.iSubItem = 1;
124 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
125 */
126
127 LoadString(hInst, ((pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_STOPPED) ? IDS_SERVICES_STATUS_STOPPED : IDS_SERVICES_STATUS_RUNNING), szStatus, 128);
128 item.pszText = szStatus;
129 item.iSubItem = 3;
130 SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
131
132 }
133 }
134
135 CloseServiceHandle(ScHandle);
136 }
137
138
139 }