Recusively delete registry keys

This is a discussion on Recusively delete registry keys within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I've been trying to delete registry keys, including all their subkeys, without resorting to SHDeleteKey (Which isn't directly Windows ...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266

    Recusively delete registry keys

    Hello,

    I've been trying to delete registry keys, including all their subkeys, without resorting to SHDeleteKey (Which isn't directly Windows 95 compatible). So far I've come up with:-
    Code:
    int DeleteKey(HKEY hKey, char *szSubKey)
    {
    	char szKeyDelete[255];
    	unsigned long ulSize = 255;
    	DWORD dwIndex = 0;
    	HKEY hKeySub;
    
    	if (!RegOpenKeyEx(hKey, szSubKey, 0, DELETE | KEY_ENUMERATE_SUB_KEYS, &hKeySub))
    	{
    		while (!RegEnumKeyEx(hKeySub, dwIndex, szKeyDelete, &ulSize, NULL, NULL, NULL, NULL))
    		{
    			DeleteKey(hKeySub, szKeyDelete);
    			ulSize = 255;
    			dwIndex++;
    		}
    
    		RegCloseKey(hKeySub);
    		RegDeleteKey(hKey, szSubKey); 
    		return TRUE;
    	}
    	else
    		return FALSE;
    
    }
    This manages to delete just one of the main subkeys of a key I'm trying to delete.

    Is there anything here that might be problematic or wrong that I'm not seeing?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You could try enumerating the keys in reverse order.

    As a side note, anyone running Windows 95 without IE4 is unlikely to be installing new software.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You could try NOT incrementing dwIndex.

    As a side not, you should make the szKeyName buffer 256 chars long, not 255, to make space for a \0-character.
    The registry is very confusing there, the programmers must have been on drugs. The lpcName parameter:
    This size should include the terminating null character. When the function returns, the variable pointed to by lpcName contains the number of characters stored in the buffer. The count returned does not include the terminating null character.
    The maximum key name size:
    The maximum size of a key name is 255 characters.
    + NUL = 256.


    And there's also this detail concerning your problem:
    While an application is using the RegEnumKeyEx function, it should not make calls to any registration functions that might change the key being enumerated.
    But I don't quite believe it.
    Last edited by CornedBee; 01-11-2004 at 05:18 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Originally posted by CornedBee
    You could try NOT incrementing dwIndex.
    You, CornedBee, are a star. Why do all my problems have such simple answers...

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    37
    Ha! You were incrementing your index while the delete call was shifting the keys down one at a time as they were being deleted! Were you skipping every other one?

    tmode -nopause

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wipe out all the keys in the "profile" registry
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2008, 04:00 AM
  2. Delete confirmation fialog Registry Key
    By Mastadex in forum Windows Programming
    Replies: 3
    Last Post: 05-30-2008, 10:48 AM
  3. Registry Keys in C++
    By Apoc in forum Windows Programming
    Replies: 1
    Last Post: 08-21-2004, 10:46 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21