locked
Clean Up registry entries. RRS feed

  • Question

  • Hi,

    I would like to write a utility which will clear all those registry entries which has a particular sub string (e.g "mycompany").

     

    So to achieve this I think i need to implement some logic which will check all those registry paths having keys which contain string/sub string "mycompany". I am trying to find out is there any API like FindNextFile() or some thing like that for registry keys.

     

    Thanks in advance.

    Wednesday, October 20, 2010 6:16 AM

Answers

  • Use RegDeleteKey Function. To delete keys recursively, use the SHDeleteKey function.

    //For exampla you want to delete HKEY_CURRENT_USER\Software\MyPersonalDir from your registry
    
    #include <windows.h>
    #include <stdio.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;
      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 TRUE;
    
      lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
    
      if (lResult != ERROR_SUCCESS) 
      {
        if (lResult == ERROR_FILE_NOT_FOUND) {
          printf("Key not found.\n");
          return TRUE;
        } 
        else {
          printf("Error opening key.\n");
          return FALSE;
        }
      }
    
      // 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;
      lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
                  NULL, NULL, &ftWrite);
    
      if (lResult == ERROR_SUCCESS) 
      {
        do {
    
          lstrcpy (lpEnd, szName);
    
          if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
            break;
          }
    
          dwSize = MAX_PATH;
    
          lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
                      NULL, NULL, &ftWrite);
    
        } while (lResult == ERROR_SUCCESS);
      }
    
      lpEnd--;
      *lpEnd = TEXT('\0');
    
      RegCloseKey (hKey);
    
      // Try again to delete the key.
    
      lResult = RegDeleteKey(hKeyRoot, lpSubKey);
    
      if (lResult == ERROR_SUCCESS) 
        return TRUE;
    
      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)
    {
      TCHAR szDelKey[2 * MAX_PATH];
    
      lstrcpy (szDelKey, lpSubKey);
      return RegDelnodeRecurse(hKeyRoot, szDelKey);
    
    }
    
    void main()
    {
      BOOL bSuccess;
    
      bSuccess = RegDelnode(HKEY_CURRENT_USER, "Software\\MyPersonalDir");
    
      if(bSuccess)
       printf("Success!\n");
      else printf("Failure.\n");
    }
    
    //After running the code, use the F5 key to refresh the registry data, and notice that the MyPersonalDir key is deleted
    
    

     Look at this KB http://support.microsoft.com/kb/142491

     

     

     


    Knowledge is like light; It spreads only when you have clear and transparent mind. - Sreedhar
    • Proposed as answer by D.Sreedhar Wednesday, October 20, 2010 6:56 AM
    • Marked as answer by Yi Feng Li Wednesday, October 27, 2010 2:04 AM
    Wednesday, October 20, 2010 6:21 AM