The Windows Registry 10

 

 

 

 

 

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

 

Creating a registry key manually

 

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(L"RegDeleteKey() - Key and subkey successfully deleted!\n");

        return TRUE;

      }

      else

      {

         wprintf(L"RegDeleteKey() - Failed to delete key and subkey! Error %d.\n", GetLastError());

         wprintf(L"Trying again..\n");

      }

 

    lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);

 

    if (lResult != ERROR_SUCCESS)

    {

        if (lResult == ERROR_FILE_NOT_FOUND)

            {

            wprintf(L"RegOpenKeyEx() - Key not found!\n");

            return TRUE;

        }

        else

            {

            wprintf(L"RegOpenKeyEx() - Error opening key, error %d\n", GetLastError());

            return FALSE;

        }

    }

      else

            wprintf(L"RegOpenKeyEx() - 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(L"RegEnumKeyEx() 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(L"RegEnumKeyEx() failed lol!\n");

 

    lpEnd--;

    *lpEnd = L'\0';

 

    if(RegCloseKey(hKey) == ERROR_SUCCESS)

            wprintf(L"hKey key was closed successfully!\n");

      else

            wprintf(L"Failed to close hKey key!\n");

 

    // Try again to delete the key.

    lResult = RegDeleteKey(hKeyRoot, lpSubKey);

 

    if (lResult == ERROR_SUCCESS)

      {

            wprintf(L"RegDeleteKey() is OK!\n");

        return TRUE;

      }

      else

            wprintf(L"RegDeleteKey() 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, L"SYSTEM\\CurrentControlSet\\Services\\Eventlog\\MyCustLogTest\\MyEventSrcName");

      // bRetVal = RegDelnode(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\Eventlog\\MyCustLogTest");

 

      // We directly give the desired key to be deleted, shouldn't to recurse

      bRetVal = RegDelnode(HKEY_CURRENT_USER, L"Software\\TestDir");

      // 0 - FALSE, Non-zero - TRUE

      wprintf(L"RegDelnode() 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.

 

Deleting Registry Key with Subkeys Program Example: A sample console program output in action, deleting registry key and subkey

 

Running the program second time generates the following output.

 

Deleting Registry Key with Subkeys Program Example: A sample console program output in action, deleting registry key and subkey that does not exist

 

The commented lines in the wmain() are the key and subkeys for the previous program example. Please try it.

 

Deleting Registry Key with Subkeys Program Example: A sample console program output in action, deleting registry key and subkey 2

 

 

 

 

< Windows Registry 9 | Windows Registry Index | Win32 Programming | Windows Registry 11 >