Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.7 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: regutil.cpp
  4. //
  5. // Module: CMSETUP.LIB
  6. //
  7. // Synopsis: Implementation of the CmDeleteRegKeyWithoutSubKeys function.
  8. //
  9. // Copyright (c) 1999 Microsoft Corporation
  10. //
  11. // Author: quintinb Created Header 08/19/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmsetup.h"
  15. //+----------------------------------------------------------------------------
  16. //
  17. // Function: CmDeleteRegKeyWithoutSubKeys
  18. //
  19. // Synopsis: This function is used to safely delete keys on both Winnt and win95.
  20. // The win95 version of RegDeleteKey will delete a key with subkeys.
  21. // In certain situations this is desirable but in others it is not.
  22. //
  23. // Arguments: HKEY hBaseKey - key that pszSubKey is relative to (usually a predefined key)
  24. // LPCTSTR pszSubKey - String containing the subkey to delete.
  25. // BOOL bIgnoreValues - if TRUE, we delete even if values exist
  26. // if FALSE, then we won't delete if values exist.
  27. //
  28. // Returns: LONG - Returns the value from RegDeleteKey unless it couldn't find the
  29. // key specified (then returns ERROR_FILE_NOT_FOUND). If the key
  30. // exists but has subkeys (or values if bIgnoreValues == FALSE), then
  31. // it returns ERROR_FILE_EXISTS.
  32. //
  33. // History: quintinb Created 9/21/98
  34. //
  35. //+----------------------------------------------------------------------------
  36. LONG CmDeleteRegKeyWithoutSubKeys(HKEY hBaseKey, LPCTSTR pszKey, BOOL bIgnoreValues)
  37. {
  38. DWORD dwSubKeys;
  39. DWORD dwValues;
  40. HKEY hKey = NULL;
  41. LPTSTR pszSubKey = NULL;
  42. LONG lReturn = ERROR_INVALID_PARAMETER;
  43. BOOL bFreePszSubKey = FALSE;
  44. if (hBaseKey && pszKey)
  45. {
  46. //
  47. // First check to see if the subkey ends with a final slash. If it
  48. // does we need to remove it because the win9x versions of the registry
  49. // APIs don't deal well with trailing slashes.
  50. //
  51. DWORD dwLen = lstrlen(pszKey);
  52. if (TEXT('\\') == pszKey[dwLen-1])
  53. {
  54. pszSubKey = (LPTSTR)CmMalloc((dwLen +1)*sizeof(TCHAR));
  55. if (pszSubKey)
  56. {
  57. lstrcpy(pszSubKey, pszKey);
  58. pszSubKey[dwLen-1] = TEXT('\0');
  59. bFreePszSubKey = TRUE; // we allocated it, so we need to delete it.
  60. }
  61. else
  62. {
  63. lReturn = ERROR_NOT_ENOUGH_MEMORY;
  64. goto exit;
  65. }
  66. }
  67. else
  68. {
  69. pszSubKey = (LPTSTR)pszKey;
  70. }
  71. //
  72. // Now open the key, check for values and subkeys, and then delete
  73. // the key if it is appropriate.
  74. //
  75. if (ERROR_SUCCESS == RegOpenKeyEx(hBaseKey, pszSubKey, 0, KEY_ALL_ACCESS, &hKey))
  76. {
  77. if (ERROR_SUCCESS == RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, NULL,
  78. NULL, &dwValues, NULL, NULL, NULL, NULL))
  79. {
  80. if ((0 == dwSubKeys) && (bIgnoreValues || (0 == dwValues)))
  81. {
  82. lReturn = RegDeleteKey(hBaseKey, pszSubKey);
  83. goto exit;
  84. }
  85. else
  86. {
  87. lReturn = ERROR_FILE_EXISTS;
  88. goto exit;
  89. }
  90. }
  91. }
  92. lReturn = ERROR_FILE_NOT_FOUND;
  93. }
  94. exit:
  95. if (hKey)
  96. {
  97. RegCloseKey(hKey);
  98. }
  99. if (bFreePszSubKey)
  100. {
  101. CmFree(pszSubKey);
  102. }
  103. return lReturn;
  104. }