Create New Registry Key (Sub Key) - Delphi

DarkCoderSc personal avatar
DarkCoderSc

Jean-Pierre LESUEUR

// ...

procedure CreateRegistrySubKey(const AHive : HKEY; const AKeyPath, ASubKeyName : String);
begin
  var AKeyHandle : HKEY;
  var AResult := RegOpenKeyExW(AHive, PWideChar(AKeyPath), 0, KEY_CREATE_SUB_KEY, AKeyHandle);
  if AResult <> ERROR_SUCCESS then
    raise EWindowsException.Create('RegOpenKeyW', AResult);
  try
    var ANewKeyHandle : HKEY;

    AResult := RegCreateKeyW(AKeyHandle, PWideChar(ASubKeyName), ANewKeyHandle);
    if AResult <> ERROR_SUCCESS then
      raise EWindowsException.Create('RegCreateKeyW', AResult);

    ///
    RegCloseKey(ANewKeyHandle);
  finally
    RegCloseKey(AKeyHandle);
  end;
end;

// ...

begin
  try
    CreateRegistrySubKey(HKEY_CURRENT_USER, 'Software', 'NewKeyName');
  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