RegDelnode.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <strsafe.h>
  5. // Stolen from the "Deleting a Key with Subkeys" example to replace
  6. // RegDeleteTree which isn't available on WinXP.
  7. // https://msdn.microsoft.com/en-us/library/ms724235(VS.85).aspx
  8. //*************************************************************
  9. //
  10. // RegDelnodeRecurse()
  11. //
  12. // Purpose: Deletes a registry key and all its subkeys / values.
  13. //
  14. // Parameters: hKeyRoot - Root key
  15. // lpSubKey - SubKey to delete
  16. //
  17. // Return: TRUE if successful.
  18. // FALSE if an error occurs.
  19. //
  20. //*************************************************************
  21. HRESULT RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey)
  22. {
  23. LPTSTR lpEnd;
  24. LONG lResult;
  25. DWORD dwSize;
  26. TCHAR szName[MAX_PATH];
  27. HKEY hKey;
  28. FILETIME ftWrite;
  29. // First, see if we can delete the key without having
  30. // to recurse.
  31. lResult = RegDeleteKey(hKeyRoot, lpSubKey);
  32. if (lResult == ERROR_SUCCESS)
  33. return lResult;
  34. lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
  35. if (lResult != ERROR_SUCCESS)
  36. return lResult;
  37. // Check for an ending slash and add one if it is missing.
  38. lpEnd = lpSubKey + lstrlen(lpSubKey);
  39. if (*(lpEnd - 1) != TEXT('\\'))
  40. {
  41. *lpEnd = TEXT('\\');
  42. lpEnd++;
  43. *lpEnd = TEXT('\0');
  44. }
  45. // Enumerate the keys
  46. dwSize = MAX_PATH;
  47. lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, nullptr,
  48. nullptr, nullptr, &ftWrite);
  49. if (lResult == ERROR_SUCCESS)
  50. {
  51. do {
  52. StringCchCopy(lpEnd, MAX_PATH * 2, szName);
  53. if (RegDelnodeRecurse(hKeyRoot, lpSubKey) != ERROR_SUCCESS) {
  54. break;
  55. }
  56. dwSize = MAX_PATH;
  57. lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, nullptr,
  58. nullptr, nullptr, &ftWrite);
  59. } while (lResult == ERROR_SUCCESS);
  60. }
  61. lpEnd--;
  62. *lpEnd = TEXT('\0');
  63. RegCloseKey(hKey);
  64. // Try again to delete the key.
  65. lResult = RegDeleteKey(hKeyRoot, lpSubKey);
  66. return lResult;
  67. }
  68. //*************************************************************
  69. //
  70. // RegDelnode()
  71. //
  72. // Purpose: Deletes a registry key and all its subkeys / values.
  73. //
  74. // Parameters: hKeyRoot - Root key
  75. // lpSubKey - SubKey to delete
  76. //
  77. // Return: TRUE if successful.
  78. // FALSE if an error occurs.
  79. //
  80. //*************************************************************
  81. HRESULT RegDelnode(HKEY hKeyRoot, LPTSTR lpSubKey)
  82. {
  83. TCHAR szDelKey[MAX_PATH * 2];
  84. StringCchCopy(szDelKey, MAX_PATH * 2, lpSubKey);
  85. return RegDelnodeRecurse(hKeyRoot, szDelKey);
  86. }