title: Registry: Delete key for each local user within system
body: <p>I got a certain registry key (created by our software) which needs to be removed on each local user account at some point.
  Thus, I try to load the users hive and then use SHDeleteKey (as the key is not empty) to get the job done. 
  However, SHDeleteKey always returns LSTATUS 2 (ERROR_FILE_NOT_FOUND).</p>
  
  <p>The Registry key for each user is placed under
  HKCU\Software\XYZ</p>
  
  <p>First, I set the required privileges within my code, which seems to work (return val is TRUE):</p>
  
  <pre><code>    (...)
  HANDLE th;
  LUID rsto;
  LUID bckp;
  TOKEN_PRIVILEGES tp;
  TOKEN_PRIVILEGES tp2;
  OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;th);
  LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &amp;rsto);
  LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &amp;bckp);
  tp.PrivilegeCount = 1;
  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  tp.Privileges[0].Luid = rsto;
  tp2.PrivilegeCount = 1;
  tp2.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  tp2.Privileges[0].Luid = bckp;
  BOOL p = AdjustTokenPrivileges(th, 0, &amp;tp, 1024, 0, 0);
  BOOL p2 = AdjustTokenPrivileges(th, 0, &amp;tp2, 1024, 0, 0);
      (...)
  </code></pre>
  
  <p>Then I use RegloadKey to load the users hive. 
  std::string connection contains the path to the respective ntuser.dat file. username is the local user account name.
  So the hive should be loaded under HKEY_USERS\username:</p>
  
  <pre><code>    (...)
  DWORD result = RegLoadKey(HKEY_USERS, username.c_str(), connection.c_str());
  return result == ERROR_SUCCESS;
      (...)
  </code></pre>
  
  <p>Now, I try to delete:</p>
  
  <pre><code>    (...)
      k = username + "\\Software\\XYZ";
      result = SHDeleteKey(HKEY_USERS, k.c_str());
  </code></pre>
  
  <p>And now result has value of 2.
  But the key exists.</p>
  
  <p>What am I doing wrong?
  Thank you in advance...</p>
  
  <p><strong>UPDATED INFO</strong>: 
  I realized the problem needs to be somewhere on RegLoadKey.
  When I load the hive via command line (REG.exe load "HKU\username" ...), I can see the node "username" under HKEY_USERS within regedit.exe. All child nodes are loaded under that node.
  When I pause my program after RegLoadKey, the node "username" is also shown under HKEY_USERS, but the node is visualized as empty, so no child nodes are available.
  How can this happen? This problem is driving me nuts.</p>