Добрый день, мои коллеги-программисты,
На всякий случай, если вы пытаетесь сделать это в C # (P / INVOKE), вот как я это сделал:
public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));
[Flags]
public enum ACCESS_MASK : uint
{
DELETE = 0x00010000,
READ_CONTROL = 0x00020000,
WRITE_DAC = 0x00040000,
WRITE_OWNER = 0x00080000,
SYNCHRONIZE = 0x00100000,
STANDARD_RIGHTS_REQUIRED = 0x000F0000,
STANDARD_RIGHTS_READ = 0x00020000,
STANDARD_RIGHTS_WRITE = 0x00020000,
STANDARD_RIGHTS_EXECUTE = 0x00020000,
STANDARD_RIGHTS_ALL = 0x001F0000,
SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
ACCESS_SYSTEM_SECURITY = 0x01000000,
MAXIMUM_ALLOWED = 0x02000000,
GENERIC_READ = 0x80000000,
GENERIC_WRITE = 0x40000000,
GENERIC_EXECUTE = 0x20000000,
GENERIC_ALL = 0x10000000
}
[Flags]
public enum REGSAM : uint
{
DELETE = 0x00010000,
READ_CONTROL = 0x00020000,
WRITE_DAC = 0x00040000,
WRITE_OWNER = 0x00080000,
KEY_QUERY_VALUE = 0x0001,
KEY_SET_VALUE = 0x0002,
KEY_CREATE_SUB_KEY = 0x0004,
KEY_ENUMERATE_SUB_KEYS = 0x0008,
KEY_NOTIFY = 0x0010,
KEY_CREATE_LINK = 0x0020,
KEY_WOW64_32KEY = 0x0200,
KEY_WOW64_64KEY = 0x0100,
KEY_WOW64_RES = 0x0300,
KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
}
[DllImport("advapi32.dll", SetLastError = true)]
static extern int RegCloseKey(
IntPtr hKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int RegOpenKeyEx(
IntPtr hKey,
string subKey,
uint ulOptions,
REGSAM samDesired,
out IntPtr hkResult);
[DllImport("ntdll.dll")]
static extern int NtRenameKey(
IntPtr KeyHandle,
ref UNICODE_STRING NewName);
[StructLayout(LayoutKind.Sequential)]
private struct UNICODE_STRING
{
public ushort Length;
public ushort MaximumLength;
[MarshalAs(UnmanagedType.LPWStr)]
public string Buffer;
public UNICODE_STRING(string value)
{
int len = value.Length;
Length = (ushort) (2 * len);
MaximumLength = (ushort) (2 * (len + 1));
Buffer = value;
}
}
public static bool RenameHklmKey(string key, string newName)
{
IntPtr keyHandle;
UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
if (0 == result)
{
result = NtRenameKey(keyHandle, ref unicodeString);
RegCloseKey(keyHandle);
}
return 0 == result;
}
перечисления были взяты из проекта Vanara
Надеюсь, это кому-нибудь поможет