Rename Registry Value - Delphi
Before Windows introduced the dedicated RegRenameKey API in Windows Vista, the most reliable and backward-compatible method for renaming a registry value was to first read the existing value, create a new value with the desired name using the data from the original value, and then delete the original value.
DarkCoderSc
Jean-Pierre LESUEUR
// ...
procedure RenameRegistryValue(const AHive : HKEY; const AKeyPath, AValueName, ANewValueName : String);
begin
var pData := nil;
var ADataSize : DWORD;
///
var AKeyHandle : HKEY;
var AResult := RegOpenKeyExW(AHive, PWideChar(AKeyPath), 0, KEY_QUERY_VALUE or KEY_SET_VALUE, AKeyHandle);
if AResult <> ERROR_SUCCESS then
raise EWindowsException.Create('RegOpenKeyW', AResult);
try
var AValueType : DWORD;
///
ReadRegistryValue(AHive, AKeyPath, AValueName, AValueType, pData, ADataSize);
SetRegistryValue(AHive, AKeyPath, ANewValueName, AValueType, pData, AdataSize);
DeleteRegistryValue(AHive, AKeyPath, AValueName);
finally
if Assigned(pData) then
FreeMem(pData, ADataSize);
///
RegCloseKey(AKeyHandle);
end;
end;
// ...
begin
try
RenameRegistryValue(HKEY_CURRENT_USER, 'Software\Test', 'Test Value', 'New Value Name');
except
on e : Exception do
WriteLn(e.Message);
end;
// ...
Depends On
Created
December 2, 2025
Last Revised
December 2, 2025