SVN maintenance
[reactos.git] / reactos / subsys / system / services / config.c
index 4c1143f..9a95f56 100644 (file)
-/*\r
- * config.c\r
- */\r
-\r
-/* INCLUDES *****************************************************************/\r
-\r
-#include "services.h"\r
-\r
-#define NDEBUG\r
-#include <debug.h>\r
-\r
-/* FUNCTIONS *****************************************************************/\r
-\r
-\r
-DWORD\r
-ScmOpenServiceKey(LPWSTR lpServiceName,\r
-                  REGSAM samDesired,\r
-                  PHKEY phKey)\r
-{\r
-    HKEY hServicesKey = NULL;\r
-    DWORD dwError;\r
-\r
-    *phKey = NULL;\r
-\r
-    dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,\r
-                            L"System\\CurrentControlSet\\Services",\r
-                            0,\r
-                            KEY_READ,\r
-                            &hServicesKey);\r
-    if (dwError != ERROR_SUCCESS)\r
-        return dwError;\r
-\r
-    dwError = RegOpenKeyExW(hServicesKey,\r
-                            lpServiceName,\r
-                            0,\r
-                            samDesired,\r
-                            phKey);\r
-\r
-    RegCloseKey(hServicesKey);\r
-\r
-    return dwError;\r
-}\r
-\r
-\r
-DWORD\r
-ScmCreateServiceKey(LPWSTR lpServiceName,\r
-                    REGSAM samDesired,\r
-                    PHKEY phKey)\r
-{\r
-    HKEY hServicesKey = NULL;\r
-    DWORD dwDisposition;\r
-    DWORD dwError;\r
-\r
-    *phKey = NULL;\r
-\r
-    dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,\r
-                            L"System\\CurrentControlSet\\Services",\r
-                            0,\r
-                            KEY_READ | KEY_CREATE_SUB_KEY,\r
-                            &hServicesKey);\r
-    if (dwError != ERROR_SUCCESS)\r
-        return dwError;\r
-\r
-    dwError = RegCreateKeyExW(hServicesKey,\r
-                              lpServiceName,\r
-                              0,\r
-                              NULL,\r
-                              REG_OPTION_NON_VOLATILE,\r
-                              samDesired,\r
-                              NULL,\r
-                              phKey,\r
-                              &dwDisposition);\r
-#if 0\r
-    if ((dwError == ERROR_SUCCESS) &&\r
-        (dwDisposition == REG_OPENED_EXISTING_KEY))\r
-    {\r
-        RegCloseKey(*phKey);\r
-        *phKey = NULL;\r
-        dwError = ERROR_SERVICE_EXISTS;\r
-    }\r
-#endif\r
-\r
-    RegCloseKey(hServicesKey);\r
-\r
-    return dwError;\r
-}\r
-\r
-\r
-\r
-DWORD\r
-ScmWriteDependencies(HKEY hServiceKey,\r
-                     LPWSTR lpDependencies,\r
-                     DWORD dwDependenciesLength)\r
-{\r
-    DWORD dwError = ERROR_SUCCESS;\r
-    DWORD dwGroupLength = 0;\r
-    DWORD dwServiceLength = 0;\r
-    DWORD dwLength;\r
-    LPWSTR lpGroupDeps;\r
-    LPWSTR lpServiceDeps;\r
-    LPWSTR lpSrc;\r
-    LPWSTR lpDst;\r
-\r
-    if (*lpDependencies == 0)\r
-    {\r
-        RegDeleteValue(hServiceKey,\r
-                       L"DependOnService");\r
-        RegDeleteValue(hServiceKey,\r
-                       L"DependOnGroup");\r
-    }\r
-    else\r
-    {\r
-        lpGroupDeps = HeapAlloc(GetProcessHeap(),\r
-                                HEAP_ZERO_MEMORY,\r
-                                (dwDependenciesLength + 2) * sizeof(WCHAR));\r
-        if (lpGroupDeps == NULL)\r
-            return ERROR_NOT_ENOUGH_MEMORY;\r
-\r
-        lpSrc = lpDependencies;\r
-        lpDst = lpGroupDeps;\r
-        while (*lpSrc != 0)\r
-        {\r
-            dwLength = wcslen(lpSrc);\r
-            if (*lpSrc == SC_GROUP_IDENTIFIERW)\r
-            {\r
-                lpSrc++;\r
-                dwGroupLength += dwLength;\r
-                wcscpy(lpDst, lpSrc);\r
-                lpDst = lpDst + dwLength;\r
-            }\r
-\r
-            lpSrc = lpSrc + dwLength;\r
-        }\r
-        *lpDst = 0;\r
-        lpDst++;\r
-        dwGroupLength++;\r
-\r
-        lpSrc = lpDependencies;\r
-        lpServiceDeps = lpDst;\r
-        while (*lpSrc != 0)\r
-        {\r
-            dwLength = wcslen(lpSrc) + 1;\r
-            if (*lpSrc != SC_GROUP_IDENTIFIERW)\r
-            {\r
-                dwServiceLength += dwLength;\r
-                wcscpy(lpDst, lpSrc);\r
-                lpDst = lpDst + dwLength;\r
-            }\r
-\r
-            lpSrc = lpSrc + dwLength;\r
-        }\r
-        *lpDst = 0;\r
-        dwServiceLength++;\r
-\r
-        dwError = RegSetValueExW(hServiceKey,\r
-                                 L"DependOnGroup",\r
-                                 0,\r
-                                 REG_MULTI_SZ,\r
-                                 (LPBYTE)lpGroupDeps,\r
-                                 dwGroupLength * sizeof(WCHAR));\r
-\r
-        if (dwError == ERROR_SUCCESS)\r
-        {\r
-            dwError = RegSetValueExW(hServiceKey,\r
-                                     L"DependOnService",\r
-                                     0,\r
-                                     REG_MULTI_SZ,\r
-                                     (LPBYTE)lpServiceDeps,\r
-                                     dwServiceLength * sizeof(WCHAR));\r
-        }\r
-\r
-        HeapFree(GetProcessHeap(), 0, lpGroupDeps);\r
-    }\r
-\r
-    return dwError;\r
-}\r
-\r
-\r
-DWORD\r
-ScmMarkServiceForDelete(PSERVICE pService)\r
-{\r
-    HKEY hServiceKey = NULL;\r
-    DWORD dwValue = 1;\r
-    DWORD dwError;\r
-\r
-    DPRINT("ScmMarkServiceForDelete() called\n");\r
-\r
-    dwError = ScmOpenServiceKey(pService->lpServiceName,\r
-                                KEY_WRITE,\r
-                                &hServiceKey);\r
-    if (dwError != ERROR_SUCCESS)\r
-        return dwError;\r
-\r
-    dwError = RegSetValueExW(hServiceKey,\r
-                             L"DeleteFlag",\r
-                             0,\r
-                             REG_DWORD,\r
-                             (LPBYTE)&dwValue,\r
-                             sizeof(DWORD));\r
-\r
-    RegCloseKey(hServiceKey);\r
-\r
-    return dwError;\r
-}\r
-\r
-\r
-BOOL\r
-ScmIsDeleteFlagSet(HKEY hServiceKey)\r
-{\r
-    DWORD dwError;\r
-    DWORD dwType;\r
-    DWORD dwFlag;\r
-    DWORD dwSize = sizeof(DWORD);\r
-\r
-    dwError = RegQueryValueExW(hServiceKey,\r
-                               L"DeleteFlag",\r
-                               0,\r
-                               &dwType,\r
-                               (LPBYTE)&dwFlag,\r
-                               &dwSize);\r
-\r
-    return (dwError == ERROR_SUCCESS);\r
-}\r
-\r
-\r
-DWORD\r
-ScmReadString(HKEY hServiceKey,\r
-              LPWSTR lpValueName,\r
-              LPWSTR *lpValue)\r
-{\r
-    DWORD dwError;\r
-    DWORD dwSize;\r
-    DWORD dwType;\r
-    DWORD dwSizeNeeded;\r
-    LPWSTR expanded = NULL;\r
-    LPWSTR ptr = NULL;\r
-\r
-    *lpValue = NULL;\r
-\r
-    dwSize = 0;\r
-    dwError = RegQueryValueExW(hServiceKey,\r
-                               lpValueName,\r
-                               0,\r
-                               &dwType,\r
-                               NULL,\r
-                               &dwSize);\r
-    if (dwError != ERROR_SUCCESS)\r
-        return dwError;\r
-\r
-    ptr = HeapAlloc(GetProcessHeap(), 0, dwSize);\r
-    if (ptr == NULL)\r
-        return ERROR_NOT_ENOUGH_MEMORY;\r
-\r
-    dwError = RegQueryValueExW(hServiceKey,\r
-                               lpValueName,\r
-                               0,\r
-                               &dwType,\r
-                               (LPBYTE)ptr,\r
-                               &dwSize);\r
-    if (dwError != ERROR_SUCCESS)\r
-        goto done;\r
-\r
-    if (dwType == REG_EXPAND_SZ)\r
-    {\r
-        /* Expand the value... */\r
-        dwSizeNeeded = ExpandEnvironmentStringsW((LPCWSTR)ptr, NULL, 0);\r
-        if (dwSizeNeeded == 0)\r
-        {\r
-            dwError = GetLastError();\r
-            goto done;\r
-        }\r
-        expanded = HeapAlloc(GetProcessHeap(), 0, dwSizeNeeded * sizeof(WCHAR));\r
-        if (dwSizeNeeded < ExpandEnvironmentStringsW((LPCWSTR)ptr, expanded, dwSizeNeeded))\r
-        {\r
-            dwError = GetLastError();\r
-            goto done;\r
-        }\r
-        *lpValue = expanded;\r
-        HeapFree(GetProcessHeap(), 0, ptr);\r
-        dwError = ERROR_SUCCESS;\r
-    }\r
-    else\r
-    {\r
-        *lpValue = ptr;\r
-    }\r
-\r
-done:;\r
-    if (dwError != ERROR_SUCCESS)\r
-    {\r
-        HeapFree(GetProcessHeap(), 0, ptr);\r
-        HeapFree(GetProcessHeap(), 0, expanded);\r
-    }\r
-\r
-    return dwError;\r
-}\r
-\r
-/* EOF */\r
-\r
+/*
+ * config.c
+ */
+
+/* INCLUDES *****************************************************************/
+
+#include "services.h"
+
+#define NDEBUG
+#include <debug.h>
+
+/* FUNCTIONS *****************************************************************/
+
+
+DWORD
+ScmOpenServiceKey(LPWSTR lpServiceName,
+                  REGSAM samDesired,
+                  PHKEY phKey)
+{
+    HKEY hServicesKey = NULL;
+    DWORD dwError;
+
+    *phKey = NULL;
+
+    dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+                            L"System\\CurrentControlSet\\Services",
+                            0,
+                            KEY_READ,
+                            &hServicesKey);
+    if (dwError != ERROR_SUCCESS)
+        return dwError;
+
+    dwError = RegOpenKeyExW(hServicesKey,
+                            lpServiceName,
+                            0,
+                            samDesired,
+                            phKey);
+
+    RegCloseKey(hServicesKey);
+
+    return dwError;
+}
+
+
+DWORD
+ScmCreateServiceKey(LPWSTR lpServiceName,
+                    REGSAM samDesired,
+                    PHKEY phKey)
+{
+    HKEY hServicesKey = NULL;
+    DWORD dwDisposition;
+    DWORD dwError;
+
+    *phKey = NULL;
+
+    dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
+                            L"System\\CurrentControlSet\\Services",
+                            0,
+                            KEY_READ | KEY_CREATE_SUB_KEY,
+                            &hServicesKey);
+    if (dwError != ERROR_SUCCESS)
+        return dwError;
+
+    dwError = RegCreateKeyExW(hServicesKey,
+                              lpServiceName,
+                              0,
+                              NULL,
+                              REG_OPTION_NON_VOLATILE,
+                              samDesired,
+                              NULL,
+                              phKey,
+                              &dwDisposition);
+#if 0
+    if ((dwError == ERROR_SUCCESS) &&
+        (dwDisposition == REG_OPENED_EXISTING_KEY))
+    {
+        RegCloseKey(*phKey);
+        *phKey = NULL;
+        dwError = ERROR_SERVICE_EXISTS;
+    }
+#endif
+
+    RegCloseKey(hServicesKey);
+
+    return dwError;
+}
+
+
+
+DWORD
+ScmWriteDependencies(HKEY hServiceKey,
+                     LPWSTR lpDependencies,
+                     DWORD dwDependenciesLength)
+{
+    DWORD dwError = ERROR_SUCCESS;
+    DWORD dwGroupLength = 0;
+    DWORD dwServiceLength = 0;
+    DWORD dwLength;
+    LPWSTR lpGroupDeps;
+    LPWSTR lpServiceDeps;
+    LPWSTR lpSrc;
+    LPWSTR lpDst;
+
+    if (*lpDependencies == 0)
+    {
+        RegDeleteValue(hServiceKey,
+                       L"DependOnService");
+        RegDeleteValue(hServiceKey,
+                       L"DependOnGroup");
+    }
+    else
+    {
+        lpGroupDeps = HeapAlloc(GetProcessHeap(),
+                                HEAP_ZERO_MEMORY,
+                                (dwDependenciesLength + 2) * sizeof(WCHAR));
+        if (lpGroupDeps == NULL)
+            return ERROR_NOT_ENOUGH_MEMORY;
+
+        lpSrc = lpDependencies;
+        lpDst = lpGroupDeps;
+        while (*lpSrc != 0)
+        {
+            dwLength = wcslen(lpSrc);
+            if (*lpSrc == SC_GROUP_IDENTIFIERW)
+            {
+                lpSrc++;
+                dwGroupLength += dwLength;
+                wcscpy(lpDst, lpSrc);
+                lpDst = lpDst + dwLength;
+            }
+
+            lpSrc = lpSrc + dwLength;
+        }
+        *lpDst = 0;
+        lpDst++;
+        dwGroupLength++;
+
+        lpSrc = lpDependencies;
+        lpServiceDeps = lpDst;
+        while (*lpSrc != 0)
+        {
+            dwLength = wcslen(lpSrc) + 1;
+            if (*lpSrc != SC_GROUP_IDENTIFIERW)
+            {
+                dwServiceLength += dwLength;
+                wcscpy(lpDst, lpSrc);
+                lpDst = lpDst + dwLength;
+            }
+
+            lpSrc = lpSrc + dwLength;
+        }
+        *lpDst = 0;
+        dwServiceLength++;
+
+        dwError = RegSetValueExW(hServiceKey,
+                                 L"DependOnGroup",
+                                 0,
+                                 REG_MULTI_SZ,
+                                 (LPBYTE)lpGroupDeps,
+                                 dwGroupLength * sizeof(WCHAR));
+
+        if (dwError == ERROR_SUCCESS)
+        {
+            dwError = RegSetValueExW(hServiceKey,
+                                     L"DependOnService",
+                                     0,
+                                     REG_MULTI_SZ,
+                                     (LPBYTE)lpServiceDeps,
+                                     dwServiceLength * sizeof(WCHAR));
+        }
+
+        HeapFree(GetProcessHeap(), 0, lpGroupDeps);
+    }
+
+    return dwError;
+}
+
+
+DWORD
+ScmMarkServiceForDelete(PSERVICE pService)
+{
+    HKEY hServiceKey = NULL;
+    DWORD dwValue = 1;
+    DWORD dwError;
+
+    DPRINT("ScmMarkServiceForDelete() called\n");
+
+    dwError = ScmOpenServiceKey(pService->lpServiceName,
+                                KEY_WRITE,
+                                &hServiceKey);
+    if (dwError != ERROR_SUCCESS)
+        return dwError;
+
+    dwError = RegSetValueExW(hServiceKey,
+                             L"DeleteFlag",
+                             0,
+                             REG_DWORD,
+                             (LPBYTE)&dwValue,
+                             sizeof(DWORD));
+
+    RegCloseKey(hServiceKey);
+
+    return dwError;
+}
+
+
+BOOL
+ScmIsDeleteFlagSet(HKEY hServiceKey)
+{
+    DWORD dwError;
+    DWORD dwType;
+    DWORD dwFlag;
+    DWORD dwSize = sizeof(DWORD);
+
+    dwError = RegQueryValueExW(hServiceKey,
+                               L"DeleteFlag",
+                               0,
+                               &dwType,
+                               (LPBYTE)&dwFlag,
+                               &dwSize);
+
+    return (dwError == ERROR_SUCCESS);
+}
+
+
+DWORD
+ScmReadString(HKEY hServiceKey,
+              LPWSTR lpValueName,
+              LPWSTR *lpValue)
+{
+    DWORD dwError;
+    DWORD dwSize;
+    DWORD dwType;
+    DWORD dwSizeNeeded;
+    LPWSTR expanded = NULL;
+    LPWSTR ptr = NULL;
+
+    *lpValue = NULL;
+
+    dwSize = 0;
+    dwError = RegQueryValueExW(hServiceKey,
+                               lpValueName,
+                               0,
+                               &dwType,
+                               NULL,
+                               &dwSize);
+    if (dwError != ERROR_SUCCESS)
+        return dwError;
+
+    ptr = HeapAlloc(GetProcessHeap(), 0, dwSize);
+    if (ptr == NULL)
+        return ERROR_NOT_ENOUGH_MEMORY;
+
+    dwError = RegQueryValueExW(hServiceKey,
+                               lpValueName,
+                               0,
+                               &dwType,
+                               (LPBYTE)ptr,
+                               &dwSize);
+    if (dwError != ERROR_SUCCESS)
+        goto done;
+
+    if (dwType == REG_EXPAND_SZ)
+    {
+        /* Expand the value... */
+        dwSizeNeeded = ExpandEnvironmentStringsW((LPCWSTR)ptr, NULL, 0);
+        if (dwSizeNeeded == 0)
+        {
+            dwError = GetLastError();
+            goto done;
+        }
+        expanded = HeapAlloc(GetProcessHeap(), 0, dwSizeNeeded * sizeof(WCHAR));
+        if (dwSizeNeeded < ExpandEnvironmentStringsW((LPCWSTR)ptr, expanded, dwSizeNeeded))
+        {
+            dwError = GetLastError();
+            goto done;
+        }
+        *lpValue = expanded;
+        HeapFree(GetProcessHeap(), 0, ptr);
+        dwError = ERROR_SUCCESS;
+    }
+    else
+    {
+        *lpValue = ptr;
+    }
+
+done:;
+    if (dwError != ERROR_SUCCESS)
+    {
+        HeapFree(GetProcessHeap(), 0, ptr);
+        HeapFree(GetProcessHeap(), 0, expanded);
+    }
+
+    return dwError;
+}
+
+/* EOF */
+