2015-02-04 21:55:15 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <strsafe.h>
|
|
|
|
|
|
|
|
// Stolen from the "Deleting a Key with Subkeys" example to replace
|
|
|
|
// RegDeleteTree which isn't available on WinXP.
|
|
|
|
// https://msdn.microsoft.com/en-us/library/ms724235(VS.85).aspx
|
|
|
|
|
|
|
|
//*************************************************************
|
|
|
|
//
|
|
|
|
// RegDelnodeRecurse()
|
|
|
|
//
|
|
|
|
// Purpose: Deletes a registry key and all its subkeys / values.
|
|
|
|
//
|
|
|
|
// Parameters: hKeyRoot - Root key
|
|
|
|
// lpSubKey - SubKey to delete
|
|
|
|
//
|
|
|
|
// Return: TRUE if successful.
|
|
|
|
// FALSE if an error occurs.
|
|
|
|
//
|
|
|
|
//*************************************************************
|
|
|
|
|
|
|
|
HRESULT RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey)
|
|
|
|
{
|
|
|
|
LPTSTR lpEnd;
|
|
|
|
LONG lResult;
|
|
|
|
DWORD dwSize;
|
|
|
|
TCHAR szName[MAX_PATH];
|
|
|
|
HKEY hKey;
|
|
|
|
FILETIME ftWrite;
|
|
|
|
|
|
|
|
// First, see if we can delete the key without having
|
|
|
|
// to recurse.
|
|
|
|
|
|
|
|
lResult = RegDeleteKey(hKeyRoot, lpSubKey);
|
|
|
|
|
|
|
|
if (lResult == ERROR_SUCCESS)
|
|
|
|
return lResult;
|
|
|
|
|
|
|
|
lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
|
|
|
|
|
|
|
|
if (lResult != ERROR_SUCCESS)
|
|
|
|
return lResult;
|
|
|
|
|
|
|
|
// Check for an ending slash and add one if it is missing.
|
|
|
|
|
|
|
|
lpEnd = lpSubKey + lstrlen(lpSubKey);
|
|
|
|
|
|
|
|
if (*(lpEnd - 1) != TEXT('\\'))
|
|
|
|
{
|
|
|
|
*lpEnd = TEXT('\\');
|
|
|
|
lpEnd++;
|
|
|
|
*lpEnd = TEXT('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enumerate the keys
|
|
|
|
|
|
|
|
dwSize = MAX_PATH;
|
2020-06-05 01:51:32 +03:00
|
|
|
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, nullptr,
|
|
|
|
nullptr, nullptr, &ftWrite);
|
2015-02-04 21:55:15 +03:00
|
|
|
|
|
|
|
if (lResult == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
|
|
|
|
StringCchCopy(lpEnd, MAX_PATH * 2, szName);
|
|
|
|
|
2015-02-12 15:34:01 +03:00
|
|
|
if (RegDelnodeRecurse(hKeyRoot, lpSubKey) != ERROR_SUCCESS) {
|
2015-02-04 21:55:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwSize = MAX_PATH;
|
|
|
|
|
2020-06-05 01:51:32 +03:00
|
|
|
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, nullptr,
|
|
|
|
nullptr, nullptr, &ftWrite);
|
2015-02-04 21:55:15 +03:00
|
|
|
|
|
|
|
} while (lResult == ERROR_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
lpEnd--;
|
|
|
|
*lpEnd = TEXT('\0');
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
|
|
|
// Try again to delete the key.
|
|
|
|
|
|
|
|
lResult = RegDeleteKey(hKeyRoot, lpSubKey);
|
|
|
|
return lResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
//*************************************************************
|
|
|
|
//
|
|
|
|
// RegDelnode()
|
|
|
|
//
|
|
|
|
// Purpose: Deletes a registry key and all its subkeys / values.
|
|
|
|
//
|
|
|
|
// Parameters: hKeyRoot - Root key
|
|
|
|
// lpSubKey - SubKey to delete
|
|
|
|
//
|
|
|
|
// Return: TRUE if successful.
|
|
|
|
// FALSE if an error occurs.
|
|
|
|
//
|
|
|
|
//*************************************************************
|
|
|
|
|
|
|
|
HRESULT RegDelnode(HKEY hKeyRoot, LPTSTR lpSubKey)
|
|
|
|
{
|
|
|
|
TCHAR szDelKey[MAX_PATH * 2];
|
|
|
|
|
|
|
|
StringCchCopy(szDelKey, MAX_PATH * 2, lpSubKey);
|
|
|
|
return RegDelnodeRecurse(hKeyRoot, szDelKey);
|
|
|
|
|
|
|
|
}
|