Guest User

Delete HKEY on NT

a guest
Nov 20th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. //Delete HKEY on NT only
  2.  
  3. DWORD RegDeleteKeyNT(
  4.       HKEY hStartKey,
  5.       LPCTSTR pKeyName
  6.       ) {
  7.  
  8.   DWORD   dwRtn, dwSubKeyLength;
  9.   //LPTSTR  pSubKey = NULL;
  10.   TCHAR   szSubKey[500];//MAX_KEY_LENGTH]; // (256) this should be dynamic.
  11.   HKEY    hKey;
  12.  
  13.   // do not allow NULL or empty key name
  14.   if (pKeyName &&  lstrlen(pKeyName)) {
  15.     if ((dwRtn=RegOpenKeyEx(hStartKey,pKeyName,
  16.       0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hKey )) == ERROR_SUCCESS) {
  17.       //int index = 0;
  18.       while (dwRtn == ERROR_SUCCESS ) {
  19.         dwSubKeyLength = 500;//MAX_KEY_LENGTH;
  20.         dwRtn=RegEnumKeyEx(
  21.                            hKey,
  22.                            0,       // always index zero
  23.                            szSubKey,
  24.                            &dwSubKeyLength,
  25.                            NULL,
  26.                            NULL,
  27.                            NULL,
  28.                            NULL
  29.                            );
  30.         if (dwRtn == ERROR_NO_MORE_ITEMS) {
  31.           dwRtn = RegDeleteKey(hStartKey, pKeyName);
  32.           break;
  33.           }
  34.         else if (dwRtn == ERROR_SUCCESS)
  35.           dwRtn=RegDeleteKeyNT(hKey, szSubKey);
  36.         }
  37.       RegCloseKey(hKey);
  38.       }
  39.     }
  40.   else
  41.     dwRtn = ERROR_BADKEY;
  42.  
  43.   return dwRtn;
  44.   }
Advertisement
Add Comment
Please, Sign In to add comment