Undocumented function “RegRenameKey”

Whilst learning some bits and pieces about the Windows API, I found a function that I wanted to use which does not appear to be documented on MSDN, “RegRenameKey”. Although Visual Studio will helpfully inform you of the datatypes required, it does not tell you what is expected within the datatypes. Since the answer doesn’t seem to be out there just yet I thought I’d do a quick writeup. The definition for the function is as follows:

LSTATUS WINAPI RegRenameKey( 
  _In_         HKEY    hKey, 
  _In_         LPCTSTR lpSubKeyName,
  _In_         LPCTSTR lpNewKeyName 
);

I assumed that lpSubKeyName and lpNewKeyName would accept the same form of input as the registry path expected for RegKeyCreate, e.g. “Software\\MyProduct\\MyKey”. However, attempting to use this returns error code 0x57 “The parameter is incorrect”. This is because lpNewKeyName seems to expect just a name without the path. A valid call looks like this:

 TCHAR keyname[] = L"Software\\testkey";
 TCHAR newkeyname[] = L"testkey2";
 LSTATUS renameerr = RegRenameKey(HKEY_CURRENT_USER, keyname, newkeyname);

Not a particularly difficult one, but hopefully this will save people some time!

2 thoughts on “Undocumented function “RegRenameKey”

  1. Pingback: Как изменить имя службы в Windows? - Компьютер76 .

  2. Max

    You don’t really need the full path, if handle is a handle of containing folder. Consider:

    string rootPath = “Software\\7-Zip”;
    string OldName = “fff”;
    string NewName = “aaa”;

    RegistryKey rootKey = Registry.CurrentUser.OpenSubKey(rootPath, true);
    int hResult = RegRenameKey(rootKey.Handle, OldName, NewName);

Comments are closed.