code improvements and various bug fixes
authorGed Murphy <gedmurphy@reactos.org>
Sat, 25 Aug 2007 13:41:44 +0000 (13:41 +0000)
committerGed Murphy <gedmurphy@reactos.org>
Sat, 25 Aug 2007 13:41:44 +0000 (13:41 +0000)
svn path=/trunk/; revision=28541

reactos/base/applications/mscutils/servman/mainwnd.c
reactos/base/applications/mscutils/servman/precomp.h
reactos/base/applications/mscutils/servman/progress.c
reactos/base/applications/mscutils/servman/query.c

index 12d8fec..f5974ed 100644 (file)
@@ -15,7 +15,7 @@ BOOL bSortAscending = TRUE;
 
 
 /* Toolbar buttons */
-TBBUTTON Buttons [NUM_BUTTONS] =
+static const TBBUTTON Buttons [] =
 {   /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
     {TBICON_PROP,    ID_PROP,    TBSTATE_INDETERMINATE, BTNS_BUTTON, {0}, 0, 0},    /* properties */
     {TBICON_REFRESH, ID_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0},          /* refresh */
@@ -141,31 +141,19 @@ VOID SetMenuAndButtonStates(PMAIN_WND_INFO Info)
 {
     HMENU hMainMenu;
     DWORD Flags, State;
+    UINT i;
 
     /* get handle to menu */
     hMainMenu = GetMenu(Info->hMainWnd);
 
     /* set all to greyed */
-    EnableMenuItem(hMainMenu, ID_START, MF_GRAYED);
-    EnableMenuItem(hMainMenu, ID_STOP, MF_GRAYED);
-    EnableMenuItem(hMainMenu, ID_PAUSE, MF_GRAYED);
-    EnableMenuItem(hMainMenu, ID_RESUME, MF_GRAYED);
-    EnableMenuItem(hMainMenu, ID_RESTART, MF_GRAYED);
-
-    EnableMenuItem(Info->hShortcutMenu, ID_START, MF_GRAYED);
-    EnableMenuItem(Info->hShortcutMenu, ID_STOP, MF_GRAYED);
-    EnableMenuItem(Info->hShortcutMenu, ID_PAUSE, MF_GRAYED);
-    EnableMenuItem(Info->hShortcutMenu, ID_RESUME, MF_GRAYED);
-    EnableMenuItem(Info->hShortcutMenu, ID_RESTART, MF_GRAYED);
-
-    SendMessage(Info->hTool, TB_SETSTATE, ID_START,
-                   (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
-    SendMessage(Info->hTool, TB_SETSTATE, ID_STOP,
-                   (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
-    SendMessage(Info->hTool, TB_SETSTATE, ID_PAUSE,
-                   (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
-    SendMessage(Info->hTool, TB_SETSTATE, ID_RESTART,
-                   (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
+    for (i = ID_START; i <= ID_RESTART; i++)
+    {
+        EnableMenuItem(hMainMenu, i, MF_GRAYED);
+        EnableMenuItem(Info->hShortcutMenu, ID_START, MF_GRAYED);
+        SendMessage(Info->hTool, TB_SETSTATE, i,
+                    (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
+    }
 
     if (Info->SelectedItem != NO_ITEM_SELECTED)
     {
@@ -249,7 +237,7 @@ CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
 static BOOL
 pCreateToolbar(PMAIN_WND_INFO Info)
 {
-    INT NumButtons = sizeof(Buttons) / sizeof(Buttons[0]);
+    INT numButtons = sizeof(Buttons) / sizeof(Buttons[0]);
 
     Info->hTool = CreateWindowEx(0,
                                  TOOLBARCLASSNAME,
@@ -288,7 +276,7 @@ pCreateToolbar(PMAIN_WND_INFO Info)
 
         SendMessage(Info->hTool,
                     TB_ADDBUTTONS,
-                    NumButtons,
+                    numButtons,
                     (LPARAM)Buttons);
 
         return TRUE;
@@ -346,7 +334,6 @@ InitListViewImage(PMAIN_WND_INFO Info)
     (void)ListView_SetImageList(Info->hListView,
                                 hLarge,
                                 LVSIL_NORMAL);
-
 }
 
 
@@ -472,7 +459,6 @@ CreateStatusBar(PMAIN_WND_INFO Info)
     if(Info->hStatus == NULL)
         return FALSE;
 
-
     SendMessage(Info->hStatus,
                 SB_SETPARTS,
                 sizeof(StatWidths) / sizeof(INT),
@@ -485,7 +471,6 @@ static VOID
 ListViewSelectionChanged(PMAIN_WND_INFO Info,
                          LPNMLISTVIEW pnmv)
 {
-
     HMENU hMainMenu;
 
     /* get handle to menu */
@@ -543,16 +528,19 @@ ListViewSelectionChanged(PMAIN_WND_INFO Info,
 }
 
 
-static VOID
+static BOOL
 InitMainWnd(PMAIN_WND_INFO Info)
 {
     if (!pCreateToolbar(Info))
+    {
         DisplayString(_T("error creating toolbar"));
+        return FALSE;
+    }
 
     if (!CreateListView(Info))
     {
         DisplayString(_T("error creating list view"));
-        return;
+        return FALSE;
     }
 
     if (!CreateStatusBar(Info))
@@ -563,6 +551,8 @@ InitMainWnd(PMAIN_WND_INFO Info)
                                    MAKEINTRESOURCE(IDR_POPUP));
     Info->hShortcutMenu = GetSubMenu(Info->hShortcutMenu,
                                      0);
+
+    return TRUE;
 }
 
 
@@ -820,7 +810,8 @@ MainWndProc(HWND hwnd,
                              GWLP_USERDATA,
                              (LONG_PTR)Info);
 
-            InitMainWnd(Info);
+            if (!InitMainWnd(Info))
+                return -1;
 
             /* Show the window */
             ShowWindow(hwnd,
@@ -1007,10 +998,9 @@ MainWndProc(HWND hwnd,
 
         case WM_CLOSE:
         {
-            /* Free service array */
             HeapFree(ProcessHeap,
                      0,
-                     Info->pServiceStatus);
+                     Info->pAllServices);
 
             DestroyMenu(Info->hShortcutMenu);
             DestroyWindow(hwnd);
@@ -1019,8 +1009,6 @@ MainWndProc(HWND hwnd,
 
         case WM_DESTROY:
         {
-            //DestroyMainWnd(Info);
-
             HeapFree(ProcessHeap,
                      0,
                      Info);
@@ -1028,7 +1016,6 @@ MainWndProc(HWND hwnd,
                              GWLP_USERDATA,
                              0);
 
-            /* Break the message queue loop */
             PostQuitMessage(0);
         }
         break;
@@ -1044,6 +1031,7 @@ HandleDefaultMessage:
         }
         break;
     }
+
     return Ret;
 }
 
@@ -1122,4 +1110,3 @@ UninitMainWindowImpl(VOID)
     UnregisterClass(szMainWndClass,
                     hInstance);
 }
-
index a7c3c62..3775be8 100644 (file)
@@ -15,9 +15,6 @@
 
 #define NO_ITEM_SELECTED -1
 #define MAX_KEY_LENGTH 256
-#define NUM_BUTTONS 11
-#define PROGRESSRANGE 8
-
 
 typedef struct _MAIN_WND_INFO
 {
@@ -28,7 +25,7 @@ typedef struct _MAIN_WND_INFO
     HMENU hShortcutMenu;
     int   nCmdShow;
 
-    ENUM_SERVICE_STATUS_PROCESS *pServiceStatus; /* Stores the complete services array */
+    ENUM_SERVICE_STATUS_PROCESS *pAllServices;
     ENUM_SERVICE_STATUS_PROCESS *CurrentService; /* Stores the current selected service */
 
     INT SelectedItem;/* selection number in the list view */
@@ -79,7 +76,7 @@ BOOL SetDescription(LPTSTR, LPTSTR);
 LPTSTR GetDescription(LPTSTR);
 LPTSTR GetExecutablePath(PMAIN_WND_INFO Info);
 BOOL RefreshServiceList(PMAIN_WND_INFO Info);
-DWORD GetServiceList(PMAIN_WND_INFO Info);
+//DWORD GetServiceList(PMAIN_WND_INFO Info);
 
 /* propsheet.c */
 LONG APIENTRY OpenPropSheet(PMAIN_WND_INFO Info);
@@ -91,35 +88,27 @@ VOID ExportFile(PMAIN_WND_INFO Info);
 INT AllocAndLoadString(OUT LPTSTR *lpTarget,
                        IN HINSTANCE hInst,
                        IN UINT uID);
-
 DWORD LoadAndFormatString(IN HINSTANCE hInstance,
                           IN UINT uID,
                           OUT LPTSTR *lpTarget,
                           ...);
-
 BOOL StatusBarLoadAndFormatString(IN HWND hStatusBar,
                                   IN INT PartId,
                                   IN HINSTANCE hInstance,
                                   IN UINT uID,
                                   ...);
-
 BOOL StatusBarLoadString(IN HWND hStatusBar,
                          IN INT PartId,
                          IN HINSTANCE hInstance,
                          IN UINT uID);
-
 INT GetTextFromEdit(OUT LPTSTR lpString,
                     IN HWND hDlg,
                     IN UINT Res);
-
 VOID GetError(VOID);
-
 VOID DisplayString(PTCHAR);
-
 HIMAGELIST InitImageList(UINT NumButtons,
                          UINT StartResource,
                          UINT Width,
                          UINT Height);
 
-
 #endif /* __SERVMAN_PRECOMP_H */
index 16a0a0f..2e37a4b 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "precomp.h"
 
+#define PROGRESSRANGE 8
+
 VOID
 CompleteProgressBar(HWND hProgDlg)
 {
index 6dcf93f..a8df4c0 100644 (file)
@@ -27,7 +27,6 @@ GetSelectedService(PMAIN_WND_INFO Info)
 }
 
 
-
 /* get vendor of service binary */
 LPTSTR
 GetExecutablePath(PMAIN_WND_INFO Info)
@@ -99,21 +98,90 @@ cleanup:
 }
 
 
+static BOOL
+GetServiceList(PMAIN_WND_INFO Info,
+               DWORD *NumServices)
+{
+    SC_HANDLE ScHandle;
+    BOOL bRet = FALSE;
+
+    DWORD BytesNeeded = 0;
+    DWORD ResumeHandle = 0;
+
+    *NumServices = 0;
+
+    ScHandle = OpenSCManager(NULL,
+                             NULL,
+                             SC_MANAGER_ENUMERATE_SERVICE);
+    if (ScHandle != INVALID_HANDLE_VALUE)
+    {
+        if (!EnumServicesStatusEx(ScHandle,
+                                  SC_ENUM_PROCESS_INFO,
+                                  SERVICE_WIN32,
+                                  SERVICE_STATE_ALL,
+                                  NULL,
+                                  0,
+                                  &BytesNeeded,
+                                  NumServices,
+                                  &ResumeHandle,
+                                  0))
+        {
+            /* Call function again if required size was returned */
+            if (GetLastError() == ERROR_MORE_DATA)
+            {
+                /* reserve memory for service info array */
+                Info->pAllServices = (ENUM_SERVICE_STATUS_PROCESS *) HeapAlloc(ProcessHeap,
+                                                                         0,
+                                                                         BytesNeeded);
+                if (Info->pAllServices)
+                {
+                    /* fill array with service info */
+                    if (EnumServicesStatusEx(ScHandle,
+                                             SC_ENUM_PROCESS_INFO,
+                                             SERVICE_WIN32,
+                                             SERVICE_STATE_ALL,
+                                             (LPBYTE)Info->pAllServices,
+                                             BytesNeeded,
+                                             &BytesNeeded,
+                                             NumServices,
+                                             &ResumeHandle,
+                                             0))
+                    {
+                        bRet = TRUE;
+                    }
+                }
+            }
+        }
+    }
+
+    if (ScHandle)
+        CloseServiceHandle(ScHandle);
+
+    if (!bRet)
+    {
+        HeapFree(ProcessHeap,
+                 0,
+                 Info->pAllServices);
+    }
+
+    return bRet;
+}
+
+
 BOOL
 RefreshServiceList(PMAIN_WND_INFO Info)
 {
+    ENUM_SERVICE_STATUS_PROCESS *pService;
     LVITEM lvItem;
     TCHAR szNumServices[32];
     TCHAR szStatus[64];
-    DWORD NumServices = 0;
+    DWORD NumServices;
     DWORD Index;
     LPCTSTR Path = _T("System\\CurrentControlSet\\Services\\%s");
 
     (void)ListView_DeleteAllItems(Info->hListView);
 
-    NumServices = GetServiceList(Info);
-
-    if (NumServices)
+    if (GetServiceList(Info, &NumServices))
     {
         TCHAR buf[300];     /* buffer to hold key path */
         INT NumListedServ = 0; /* how many services were listed */
@@ -122,38 +190,43 @@ RefreshServiceList(PMAIN_WND_INFO Info)
         {
             HKEY hKey = NULL;
             LPTSTR lpDescription = NULL;
-            LPTSTR LogOnAs = NULL;
+            LPTSTR lpLogOnAs = NULL;
             DWORD StartUp = 0;
             DWORD dwValueSize;
 
+            /* copy the service info over */
+            pService = &Info->pAllServices[Index];
+
              /* open the registry key for the service */
             _sntprintf(buf,
                        300,
                        Path,
-                       Info->pServiceStatus[Index].lpServiceName);
-
-            RegOpenKeyEx(HKEY_LOCAL_MACHINE,
-                         buf,
+                       pService->lpServiceName);
+            if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                             buf,
+                             0,
+                             KEY_READ,
+                             &hKey) != ERROR_SUCCESS)
+            {
+                HeapFree(ProcessHeap,
                          0,
-                         KEY_READ,
-                         &hKey);
-
+                         pService);
+                continue;
+            }
 
             /* set the display name */
             ZeroMemory(&lvItem, sizeof(LVITEM));
             lvItem.mask = LVIF_TEXT | LVIF_PARAM;
-            lvItem.pszText = Info->pServiceStatus[Index].lpDisplayName;
+            lvItem.pszText = pService->lpDisplayName;
 
-            /* Set a pointer for each service so we can query it later.
-             * Not all services are added to the list, so we can't query
-             * the item number as they become out of sync with the array */
-            lvItem.lParam = (LPARAM)&Info->pServiceStatus[Index];
+            /* Add the service pointer */
+            lvItem.lParam = (LPARAM)pService;
 
-            lvItem.iItem = ListView_GetItemCount(Info->hListView);
+            /* add it to the listview */
             lvItem.iItem = ListView_InsertItem(Info->hListView, &lvItem);
 
             /* set the description */
-            if ((lpDescription = GetDescription(Info->pServiceStatus[Index].lpServiceName)))
+            if ((lpDescription = GetDescription(pService->lpServiceName)))
             {
                 lvItem.pszText = lpDescription;
                 lvItem.iSubItem = 1;
@@ -168,7 +241,7 @@ RefreshServiceList(PMAIN_WND_INFO Info)
             }
 
             /* set the status */
-            if (Info->pServiceStatus[Index].ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
+            if (pService->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
             {
                 LoadString(hInstance,
                            IDS_SERVICES_STARTED,
@@ -189,44 +262,34 @@ RefreshServiceList(PMAIN_WND_INFO Info)
                                 NULL,
                                 NULL,
                                 (LPBYTE)&StartUp,
-                                &dwValueSize))
+                                &dwValueSize) == ERROR_SUCCESS)
             {
-                RegCloseKey(hKey);
-                continue;
-            }
+                switch (StartUp)
+                {
+                    case 2:
+                        LoadStringW(hInstance,
+                                    IDS_SERVICES_AUTO,
+                                    szStatus,
+                                    sizeof(szStatus) / sizeof(TCHAR));
+                         break;
+                    case 3:
+                        LoadStringW(hInstance,
+                                    IDS_SERVICES_MAN,
+                                    szStatus,
+                                    sizeof(szStatus) / sizeof(TCHAR));
+                        break;
+
+                    case 4:
+                        LoadStringW(hInstance,
+                                    IDS_SERVICES_DIS,
+                                    szStatus,
+                                    sizeof(szStatus) / sizeof(TCHAR));
+                        break;
+                    default:
+                        szStatus[0] = 0;
+                        break;
+                }
 
-            if (StartUp == 0x02)
-            {
-                LoadString(hInstance,
-                           IDS_SERVICES_AUTO,
-                           szStatus,
-                           sizeof(szStatus) / sizeof(TCHAR));
-                lvItem.pszText = szStatus;
-                lvItem.iSubItem = 3;
-                SendMessage(Info->hListView,
-                            LVM_SETITEMTEXT,
-                            lvItem.iItem,
-                            (LPARAM)&lvItem);
-            }
-            else if (StartUp == 0x03)
-            {
-                LoadString(hInstance,
-                           IDS_SERVICES_MAN,
-                           szStatus,
-                           sizeof(szStatus) / sizeof(TCHAR));
-                lvItem.pszText = szStatus;
-                lvItem.iSubItem = 3;
-                SendMessage(Info->hListView,
-                            LVM_SETITEMTEXT,
-                            lvItem.iItem,
-                            (LPARAM)&lvItem);
-            }
-            else if (StartUp == 0x04)
-            {
-                LoadString(hInstance,
-                           IDS_SERVICES_DIS,
-                           szStatus,
-                           sizeof(szStatus) / sizeof(TCHAR));
                 lvItem.pszText = szStatus;
                 lvItem.iSubItem = 3;
                 SendMessage(Info->hListView,
@@ -242,52 +305,39 @@ RefreshServiceList(PMAIN_WND_INFO Info)
                                 NULL,
                                 NULL,
                                 NULL,
-                                &dwValueSize))
+                                &dwValueSize) == ERROR_SUCCESS)
             {
-                RegCloseKey(hKey);
-                continue;
-            }
+                lpLogOnAs = HeapAlloc(ProcessHeap,
+                                     0,
+                                     dwValueSize);
+                if (lpLogOnAs != NULL)
+                {
+                    if(RegQueryValueEx(hKey,
+                                       _T("ObjectName"),
+                                       NULL,
+                                       NULL,
+                                       (LPBYTE)lpLogOnAs,
+                                       &dwValueSize) == ERROR_SUCCESS)
+                    {
+                        lvItem.pszText = lpLogOnAs;
+                        lvItem.iSubItem = 4;
+                        SendMessage(Info->hListView,
+                                    LVM_SETITEMTEXT,
+                                    lvItem.iItem,
+                                    (LPARAM)&lvItem);
+                    }
+
+                    HeapFree(ProcessHeap,
+                             0,
+                             lpLogOnAs);
+                }
 
-            LogOnAs = HeapAlloc(ProcessHeap,
-                                HEAP_ZERO_MEMORY,
-                                dwValueSize);
-            if (LogOnAs == NULL)
-            {
                 RegCloseKey(hKey);
-                return FALSE;
             }
-            if(RegQueryValueEx(hKey,
-                               _T("ObjectName"),
-                               NULL,
-                               NULL,
-                               (LPBYTE)LogOnAs,
-                               &dwValueSize))
-            {
-                HeapFree(ProcessHeap,
-                         0,
-                         LogOnAs);
-                RegCloseKey(hKey);
-                continue;
-            }
-
-            lvItem.pszText = LogOnAs;
-            lvItem.iSubItem = 4;
-            SendMessage(Info->hListView,
-                        LVM_SETITEMTEXT,
-                        lvItem.iItem,
-                        (LPARAM)&lvItem);
-
-            HeapFree(ProcessHeap,
-                     0,
-                     LogOnAs);
-
-            RegCloseKey(hKey);
-
         }
 
-        NumListedServ = ListView_GetItemCount(Info->hListView);
-
         /* set the number of listed services in the status bar */
+        NumListedServ = ListView_GetItemCount(Info->hListView);
         LoadString(hInstance,
                    IDS_NUM_SERVICES,
                    szNumServices,
@@ -308,78 +358,7 @@ RefreshServiceList(PMAIN_WND_INFO Info)
     SendMessage (Info->hListView,
                  WM_SETREDRAW,
                  TRUE,
-                 0) ;
+                 0);
 
     return TRUE;
 }
-
-
-
-
-DWORD
-GetServiceList(PMAIN_WND_INFO Info)
-{
-    SC_HANDLE ScHandle;
-    BOOL bGotServices = FALSE;
-
-    DWORD BytesNeeded = 0;
-    DWORD ResumeHandle = 0;
-    DWORD NumServices = 0;
-
-    ScHandle = OpenSCManager(NULL,
-                             NULL,
-                             SC_MANAGER_ENUMERATE_SERVICE);
-    if (ScHandle != INVALID_HANDLE_VALUE)
-    {
-        if (!EnumServicesStatusEx(ScHandle,
-                                  SC_ENUM_PROCESS_INFO,
-                                  SERVICE_WIN32,
-                                  SERVICE_STATE_ALL,
-                                  (LPBYTE)Info->pServiceStatus,
-                                  0,
-                                  &BytesNeeded,
-                                  &NumServices,
-                                  &ResumeHandle,
-                                  0))
-        {
-            /* Call function again if required size was returned */
-            if (GetLastError() == ERROR_MORE_DATA)
-            {
-                /* reserve memory for service info array */
-                Info->pServiceStatus = (ENUM_SERVICE_STATUS_PROCESS *)
-                                          HeapAlloc(ProcessHeap,
-                                                    0,
-                                                    BytesNeeded);
-                if (Info->pServiceStatus == NULL)
-                    return FALSE;
-
-                /* fill array with service info */
-                if (EnumServicesStatusEx(ScHandle,
-                                         SC_ENUM_PROCESS_INFO,
-                                         SERVICE_WIN32,
-                                         SERVICE_STATE_ALL,
-                                         (LPBYTE)Info->pServiceStatus,
-                                         BytesNeeded,
-                                         &BytesNeeded,
-                                         &NumServices,
-                                         &ResumeHandle,
-                                         0))
-                {
-                    bGotServices = TRUE;
-                }
-            }
-        }
-    }
-
-    if (ScHandle)
-        CloseServiceHandle(ScHandle);
-
-    if (!bGotServices)
-    {
-        HeapFree(ProcessHeap,
-                 0,
-                 Info->pServiceStatus);
-    }
-
-    return NumServices;
-}