Forum Discussion
HKosova
Alumni
12 years agoHi Howie,
One note about the .NET method: by default, it accesses only the 32-bit or only 64-bit registry - as specified by the Preferred architecture of the assembly hosting process option in the CLR Bridge options of the current project. If you need to access both 32-bit and 64-bit registries at the same time (e.g. in HKLM\SOFTWARE\ and HKLM \SOFTWARE\Wow6432Node), use something like this:
(Note: This requires .NET Framework 4+.)
One note about the .NET method: by default, it accesses only the 32-bit or only 64-bit registry - as specified by the Preferred architecture of the assembly hosting process option in the CLR Bridge options of the current project. If you need to access both 32-bit and 64-bit registries at the same time (e.g. in HKLM\SOFTWARE\ and HKLM \SOFTWARE\Wow6432Node), use something like this:
(Note: This requires .NET Framework 4+.)
function Test()
{
var HKLM = -2147483646;
var key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
var value = "ProgramFilesDir";
var str = GetRegistryValue(HKLM, key, value /* , 32 */);
Log.Message(str); // C:\Program Files (x86) - value from Wow6432Node
str = GetRegistryValue(HKLM, key, value, 64);
Log.Message(str); // C:\Program Files
}
function GetRegistryValue(RootKey, KeyName, ValueName, Bitness)
{
var oRegView = (Bitness == 64
? dotNET.Microsoft_Win32.RegistryView.Registry64
: dotNET.Microsoft_Win32.RegistryView.Registry32);
var oRoot = dotNET.Microsoft_Win32.RegistryKey.OpenBaseKey(RootKey, oRegView);
var oKey = oRoot.OpenSubKey(KeyName);
return oKey.GetValue(ValueName);
}