Registry - How to rename key in registry using C++?
up vote
3
down vote
favorite
How to rename key in registry using C++?
I want rename key "MyappVersion1" to "MyappVersion2".
I don't see any function in MSDN about renaming keys in registry.
c++ c winapi registry
add a comment |
up vote
3
down vote
favorite
How to rename key in registry using C++?
I want rename key "MyappVersion1" to "MyappVersion2".
I don't see any function in MSDN about renaming keys in registry.
c++ c winapi registry
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
How to rename key in registry using C++?
I want rename key "MyappVersion1" to "MyappVersion2".
I don't see any function in MSDN about renaming keys in registry.
c++ c winapi registry
How to rename key in registry using C++?
I want rename key "MyappVersion1" to "MyappVersion2".
I don't see any function in MSDN about renaming keys in registry.
c++ c winapi registry
c++ c winapi registry
asked Oct 4 '09 at 12:35
Jasmin25
9217
9217
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58
add a comment |
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58
add a comment |
6 Answers
6
active
oldest
votes
up vote
4
down vote
accepted
There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
add a comment |
up vote
7
down vote
If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
add a comment |
up vote
3
down vote
if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to useRegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
add a comment |
up vote
3
down vote
Some more info on this old entry:
NTRenameKey() is an old API for renaming a registry key.
Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.
[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
add a comment |
up vote
2
down vote
I know this is an old entry, but in case anyone else comes looking, like me ...
In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName)
available from Vista onwards.
add a comment |
up vote
0
down vote
Good day my fellow programmers,
Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:
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;
}
enums were taken from Vanara project
Hope it helps someone
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
add a comment |
up vote
4
down vote
accepted
There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.
There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.
answered Oct 4 '09 at 12:51
Anders
68.7k1074127
68.7k1074127
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
add a comment |
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
Thank you anders :)
– Jasmin25
Oct 4 '09 at 13:05
add a comment |
up vote
7
down vote
If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
add a comment |
up vote
7
down vote
If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
add a comment |
up vote
7
down vote
up vote
7
down vote
If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().
If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().
answered Oct 4 '09 at 12:49
Ferruccio
79.2k34198284
79.2k34198284
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
add a comment |
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
Thanks Ferruccio, I will implement that :)
– Jasmin25
Oct 4 '09 at 13:06
add a comment |
up vote
3
down vote
if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to useRegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
add a comment |
up vote
3
down vote
if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to useRegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
add a comment |
up vote
3
down vote
up vote
3
down vote
if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx
if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx
answered Dec 8 '11 at 21:38
Bevan Collins
1,1671220
1,1671220
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to useRegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
add a comment |
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to useRegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
1
1
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
RegDeleteKeyEx
is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
– c00000fd
Apr 11 '17 at 5:42
add a comment |
up vote
3
down vote
Some more info on this old entry:
NTRenameKey() is an old API for renaming a registry key.
Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.
[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
add a comment |
up vote
3
down vote
Some more info on this old entry:
NTRenameKey() is an old API for renaming a registry key.
Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.
[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
add a comment |
up vote
3
down vote
up vote
3
down vote
Some more info on this old entry:
NTRenameKey() is an old API for renaming a registry key.
Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.
[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
Some more info on this old entry:
NTRenameKey() is an old API for renaming a registry key.
Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.
[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);
answered May 8 '17 at 17:58
Aaron
513
513
add a comment |
add a comment |
up vote
2
down vote
I know this is an old entry, but in case anyone else comes looking, like me ...
In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName)
available from Vista onwards.
add a comment |
up vote
2
down vote
I know this is an old entry, but in case anyone else comes looking, like me ...
In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName)
available from Vista onwards.
add a comment |
up vote
2
down vote
up vote
2
down vote
I know this is an old entry, but in case anyone else comes looking, like me ...
In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName)
available from Vista onwards.
I know this is an old entry, but in case anyone else comes looking, like me ...
In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName)
available from Vista onwards.
answered Aug 25 '15 at 22:40
randomsock
42525
42525
add a comment |
add a comment |
up vote
0
down vote
Good day my fellow programmers,
Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:
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;
}
enums were taken from Vanara project
Hope it helps someone
add a comment |
up vote
0
down vote
Good day my fellow programmers,
Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:
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;
}
enums were taken from Vanara project
Hope it helps someone
add a comment |
up vote
0
down vote
up vote
0
down vote
Good day my fellow programmers,
Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:
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;
}
enums were taken from Vanara project
Hope it helps someone
Good day my fellow programmers,
Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:
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;
}
enums were taken from Vanara project
Hope it helps someone
edited Nov 19 at 8:29
answered Nov 15 at 11:52
Gregory Stein
1069
1069
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1516312%2fregistry-how-to-rename-key-in-registry-using-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58