[CMAKE]
[reactos.git] / base / applications / mscutils / servman / query.c
index 3c00785..a06ef58 100644 (file)
@@ -30,63 +30,54 @@ GetSelectedService(PMAIN_WND_INFO Info)
 LPQUERY_SERVICE_CONFIG
 GetServiceConfig(LPTSTR lpServiceName)
 {
-    SC_HANDLE hSCManager = NULL;
-    SC_HANDLE hSc = NULL;
-    LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
-    DWORD BytesNeeded = 0;
+    LPQUERY_SERVICE_CONFIG lpServiceConfig = NULL;
+    SC_HANDLE hSCManager;
+    SC_HANDLE hService;
+    DWORD dwBytesNeeded;
 
     hSCManager = OpenSCManager(NULL,
                                NULL,
-                               SC_MANAGER_ENUMERATE_SERVICE);
-    if (hSCManager == NULL)
-    {
-        GetError();
-        return NULL;
-    }
-
-    hSc = OpenService(hSCManager,
-                      lpServiceName,
-                      SERVICE_QUERY_CONFIG);
-    if (hSc == NULL)
-    {
-        GetError();
-        goto cleanup;
-    }
-
-    if (!QueryServiceConfig(hSc,
-                            pServiceConfig,
-                            0,
-                            &BytesNeeded))
+                               SC_MANAGER_ALL_ACCESS);
+    if (hSCManager)
     {
-        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
+        hService = OpenService(hSCManager,
+                               lpServiceName,
+                               SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS | SERVICE_QUERY_CONFIG);
+        if (hService)
         {
-            pServiceConfig = (LPQUERY_SERVICE_CONFIG) HeapAlloc(ProcessHeap,
-                                                                0,
-                                                                BytesNeeded);
-            if (pServiceConfig == NULL)
-                goto cleanup;
-
-            if (!QueryServiceConfig(hSc,
-                                    pServiceConfig,
-                                    BytesNeeded,
-                                    &BytesNeeded))
+            if (!QueryServiceConfig(hService,
+                                    NULL,
+                                    0,
+                                    &dwBytesNeeded))
             {
-                HeapFree(ProcessHeap,
-                         0,
-                         pServiceConfig);
-
-                pServiceConfig = NULL;
+                if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
+                {
+                    lpServiceConfig = (LPQUERY_SERVICE_CONFIG)HeapAlloc(GetProcessHeap(),
+                                                                        0,
+                                                                        dwBytesNeeded);
+                    if (lpServiceConfig)
+                    {
+                        if (!QueryServiceConfig(hService,
+                                                lpServiceConfig,
+                                                dwBytesNeeded,
+                                                &dwBytesNeeded))
+                        {
+                            HeapFree(GetProcessHeap(),
+                                     0,
+                                     lpServiceConfig);
+                            lpServiceConfig = NULL;
+                        }
+                    }
+                }
             }
+
+            CloseServiceHandle(hService);
         }
-    }
 
-cleanup:
-    if (hSCManager != NULL)
         CloseServiceHandle(hSCManager);
-    if (hSc != NULL)
-        CloseServiceHandle(hSc);
+    }
 
-    return pServiceConfig;
+    return lpServiceConfig;
 }
 
 
@@ -152,6 +143,7 @@ GetServiceDescription(LPTSTR lpServiceName)
     SERVICE_DESCRIPTION *pServiceDescription = NULL;
     LPTSTR lpDescription = NULL;
     DWORD BytesNeeded = 0;
+    DWORD dwSize;
 
     hSCManager = OpenSCManager(NULL,
                                NULL,
@@ -189,12 +181,16 @@ GetServiceDescription(LPTSTR lpServiceName)
                 {
                     if (pServiceDescription->lpDescription)
                     {
+                        dwSize = _tcslen(pServiceDescription->lpDescription) + 1;
                         lpDescription = HeapAlloc(ProcessHeap,
                                                   0,
-                                                  (_tcslen(pServiceDescription->lpDescription) + 1) * sizeof(TCHAR));
+                                                  dwSize * sizeof(TCHAR));
                         if (lpDescription)
-                            _tcscpy(lpDescription,
-                                    pServiceDescription->lpDescription);
+                        {
+                            _tcscpy_s(lpDescription,
+                                      dwSize,
+                                      pServiceDescription->lpDescription);
+                        }
                     }
                 }
             }