Deleting Registry Key with Subkeys Program Example
The following program example uses the RegOpenKeyEx(), RegEnumKeyEx(), and RegDeleteKey() functions to delete a registry key with subkeys. To test this example, create the following registry key by using Regedt32.exe manually, and then add a few values and subkeys if you want.
HKEY_CURRENT_USER\Software\TestDir
Or you can modify the related part of the source code to delete the previously created registry key.
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
#include <windows.h>
#include <stdio.h>
#include <strsafe.h>
//*************************************************************
// RegDelnodeRecurse()
// Purpose: Deletes a registry key and all it's subkeys / values.
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
// Return: TRUE if successful.
// FALSE if an error occurs.
//*************************************************************
BOOL RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey)
{
LPTSTR lpEnd;
LONG lResult;
DWORD dwSize;
WCHAR 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)
{
wprintf(LRegDeleteKey() - Key and subkey successfully deleted!\n);
return TRUE;
}
else
{
wprintf(LRegDeleteKey() - Failed to delete key and subkey! Error %d.\n, GetLastError());
wprintf(LTrying again..\n);
}
lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
if (lResult != ERROR_SUCCESS)
{
if (lResult == ERROR_FILE_NOT_FOUND)
{
wprintf(LRegOpenKeyEx() - Key not found!\n);
return TRUE;
}
else
{
wprintf(LRegOpenKeyEx() - Error opening key, error %d\n, GetLastError());
return FALSE;
}
}
else
wprintf(LRegOpenKeyEx() - Key opened successfully!\n);
// Check for an ending slash and add one if it is missing
lpEnd = lpSubKey + lstrlen(lpSubKey);
if (*(lpEnd - 1) != L'\\')
{
*lpEnd = L'\\';
lpEnd++;
*lpEnd = L'\0';
}
// Enumerate the keys
dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite);
if (lResult == ERROR_SUCCESS)
{
wprintf(LRegEnumKeyEx() is pretty fine!\n);
do {
StringCchCopy(lpEnd, MAX_PATH*2, szName);
if (!RegDelnodeRecurse(hKeyRoot, lpSubKey))
{
break;
}
dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite);
} while (lResult == ERROR_SUCCESS);
}
else
wprintf(LRegEnumKeyEx() failed lol!\n);
lpEnd--;
*lpEnd = L'\0';
if(RegCloseKey(hKey) == ERROR_SUCCESS)
wprintf(LhKey key was closed successfully!\n);
else
wprintf(LFailed to close hKey key!\n);
// Try again to delete the key.
lResult = RegDeleteKey(hKeyRoot, lpSubKey);
if (lResult == ERROR_SUCCESS)
{
wprintf(LRegDeleteKey() is OK!\n);
return TRUE;
}
else
wprintf(LRegDeleteKey() failed!\n);
return FALSE;
}
//*************************************************************
// RegDelnode()
// Purpose: Deletes a registry key and all it's subkeys / values.
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
// Return: TRUE if successful.
// FALSE if an error occurs.
//*************************************************************
BOOL RegDelnode(HKEY hKeyRoot, LPTSTR lpSubKey)
{
WCHAR szDelKey[MAX_PATH*2];
StringCchCopy(szDelKey, MAX_PATH*2, lpSubKey);
// Recurse starts from root key, HKEY_CLASSES_ROOT
return RegDelnodeRecurse(hKeyRoot, szDelKey);
}
int wmain(int argc, WCHAR *argv[])
{
BOOL bRetVal;
// The first two are the key and subkeys for the previous program example. Note the variation
// bRetVal = RegDelnode(HKEY_LOCAL_MACHINE, LSYSTEM\\CurrentControlSet\\Services\\Eventlog\\MyCustLogTest\\MyEventSrcName);
// bRetVal = RegDelnode(HKEY_LOCAL_MACHINE, LSYSTEM\\CurrentControlSet\\Services\\Eventlog\\MyCustLogTest);
// We directly give the desired key to be deleted, shouldn't to recurse
bRetVal = RegDelnode(HKEY_CURRENT_USER, LSoftware\\TestDir);
// 0 - FALSE, Non-zero - TRUE
wprintf(LRegDelnode() returns %d\n, bRetVal);
return 0;
}
Build and run the project. The following screenshot is a sample output.
After running the following code, use the F5 key to refresh (Registry Editor) the registry data, and notice that the TestDir key was deleted.
Running the program second time generates the following output.
The commented lines in the wmain() are the key and subkeys for the previous program example. Please try it.