Rename Registry Key - Delphi

DarkCoderSc personal avatar
DarkCoderSc

Jean-Pierre LESUEUR

// ...

function RegRenameKey(hKey : HKEY; lpSubKeyName, lpNewKeyName : String) : Longint; stdcall; external 'Advapi32.dll'

// ...

procedure RenameRegistryKey(const AHive : HKEY; const AKeyPath, ASubKeyName, ANewKeyName : String);
begin
  var AKeyHandle : HKEY;
  var AResult := RegOpenKeyExW(AHive, PWideChar(AKeyPath), 0, KEY_WRITE, AKeyHandle);
  if AResult <> ERROR_SUCCESS then
    raise EWindowsException.Create('RegOpenKeyW', AResult);
  try
    AResult := RegRenameKey(AKeyHandle, PWideChar(ASubKeyName), PWideChar(ANewKeyName));
    if AResult <> ERROR_SUCCESS then
      raise EWindowsException.Create('RegRenameKey', AResult);
  finally
    RegCloseKey(AKeyHandle);
  end;
end;


// ...

begin
  try
    RenameRegistryKey(HKEY_CURRENT_USER, 'Software', 'Test', 'Renamed Test');
  except
    on e : Exception do
      WriteLn(e.Message);
  end;

// ...

Creating and researching code snippets takes time and effort. You’re welcome to share them through your own platforms, but please don’t forget to credit the original author, here: Jean-Pierre LESUEUR.

no AI logo


Depends On


Created

December 2, 2025

Last Revised

December 2, 2025