list services with EnumServicesStatusEx instead of reading them from registry
[reactos.git] / reactos / subsys / system / msconfig / srvpage.c
index 36bd3ce..825f03b 100644 (file)
@@ -33,17 +33,23 @@ ServicesPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
         column.cx = 200;
         ListView_InsertColumn(hServicesListCtrl, 0, &column);
 
+        column.mask = LVCF_TEXT | LVCF_WIDTH;
+        LoadString(hInst, IDS_SERVICES_COLUMN_REQ, szTemp, 256);
+        column.pszText = szTemp;
+        column.cx = 70;
+        ListView_InsertColumn(hServicesListCtrl, 1, &column);
+
         column.mask = LVCF_TEXT | LVCF_WIDTH;
         LoadString(hInst, IDS_SERVICES_COLUMN_VENDOR, szTemp, 256);
         column.pszText = szTemp;
         column.cx = 200;
-        ListView_InsertColumn(hServicesListCtrl, 1, &column);
+        ListView_InsertColumn(hServicesListCtrl, 2, &column);
 
         column.mask = LVCF_TEXT | LVCF_WIDTH;
         LoadString(hInst, IDS_SERVICES_COLUMN_STATUS, szTemp, 256);
         column.pszText = szTemp;
         column.cx = 70;
-        ListView_InsertColumn(hServicesListCtrl, 2, &column);
+        ListView_InsertColumn(hServicesListCtrl, 3, &column);
 
         GetServices();
                return TRUE;
@@ -55,50 +61,79 @@ ServicesPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
 void
 GetServices ( void )
 {
-    HKEY hKey, hSubKey;
-    DWORD dwSubKeys, dwKeyLength;
-    DWORD dwType, dwDataLength;
-    size_t Index;
-    TCHAR lpKeyName[MAX_KEY_LENGTH];
-    TCHAR lpSubKey[MAX_KEY_LENGTH];
-    TCHAR DisplayName[MAX_VALUE_NAME];
-    TCHAR ObjectName[MAX_VALUE_NAME];
-    TCHAR lpServicesKey[MAX_KEY_LENGTH] = _T("SYSTEM\\CurrentControlSet\\Services");
     LV_ITEM item;
+    SC_HANDLE ScHandle;
+    DWORD BufSize = 0;
+    DWORD BytesNeeded = 0;
+    DWORD ResumeHandle = 0;
+    DWORD NumServices = 0; 
+    size_t Index;
+    TCHAR szStatus[128];
 
-    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpServicesKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
+    ENUM_SERVICE_STATUS_PROCESS *pServiceStatus = NULL;
+
+    ScHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
+    if (ScHandle != INVALID_HANDLE_VALUE)
     {
-        if (RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
+        if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BufSize, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
+        {
+            /* Call function again if required size was returned */
+            if (GetLastError() == ERROR_MORE_DATA)
+            {
+                /* reserve memory for service info array */
+                pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
+                if (pServiceStatus == NULL) 
+                               return;
+
+                /* fill array with service info */
+                if (EnumServicesStatusEx(ScHandle, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pServiceStatus, BytesNeeded, &BytesNeeded, &NumServices, &ResumeHandle, 0) == 0)
+                {
+                    HeapFree(GetProcessHeap(), 0, pServiceStatus);
+                    return;
+                }
+            }
+            else /* exit on failure */
+            {
+                return;
+            }
+        }
+
+        if (NumServices)
         {
-            for (Index = 0; Index < dwSubKeys; Index++) 
-            { 
-                dwKeyLength = MAX_KEY_LENGTH;
-                if (RegEnumKeyEx(hKey, Index, lpKeyName, &dwKeyLength, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
+            for (Index = 0; Index < NumServices; Index++)
+            {
+                memset(&item, 0, sizeof(LV_ITEM));
+                item.mask = LVIF_TEXT;
+                item.iImage = 0;
+                item.pszText = pServiceStatus[Index].lpDisplayName;
+                item.iItem = ListView_GetItemCount(hServicesListCtrl);
+                item.lParam = 0;
+                item.iItem = ListView_InsertItem(hServicesListCtrl, &item);
+
+                /* FIXME
+                if (QueryServiceConfig2(ScHandle, SERVICE_CONFIG_FAILURE_ACTIONS, ) == 0)
                 {
-                    _tcscpy(lpSubKey, lpServicesKey);
-                    _tcscat(lpSubKey, _T("\\"));
-                    _tcscat(lpSubKey, lpKeyName);
-                    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
+                    if (GetLastError() == ERROR_MORE_DATA)
                     {
-                        dwDataLength = MAX_VALUE_NAME;
-                        if (RegQueryValueEx(hSubKey, _T("ObjectName"), NULL, &dwType, (LPBYTE)ObjectName, &dwDataLength) == ERROR_SUCCESS)
-                        {
-                            dwDataLength = MAX_VALUE_NAME;
-                            if (RegQueryValueEx(hSubKey, _T("DisplayName"), NULL, &dwType, (LPBYTE)DisplayName, &dwDataLength) == ERROR_SUCCESS)
-                            {
-                                memset(&item, 0, sizeof(LV_ITEM));
-                                item.mask = LVIF_TEXT;
-                                item.iImage = 0;
-                                item.pszText = DisplayName;
-                                item.iItem = ListView_GetItemCount(hServicesListCtrl);
-                                item.lParam = 0;
-                                ListView_InsertItem(hServicesListCtrl, &item);
-                            }
-                        }
+
                     }
                 }
+                LoadString(hInst, ( CONDITION  ? IDS_YES : IDS_NO), szStatus, 128);
+                item.pszText = szStatus;
+                item.iSubItem = 1;
+                SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
+                */
+
+                LoadString(hInst, ((pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_STOPPED) ? IDS_SERVICES_STATUS_STOPPED : IDS_SERVICES_STATUS_RUNNING), szStatus, 128);
+                item.pszText = szStatus;
+                item.iSubItem = 3;
+                SendMessage(hServicesListCtrl, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
+
             }
         }
-        RegCloseKey(hKey);
-    }
+
+        CloseServiceHandle(ScHandle);
+    }        
+
+
 }