Accessing the Windows registry using .NET?

I find strange behavior with the .NET module by accessing the Windows Registry using the RegistryKey class.

For example, I wrote a testcom.dll .NET module that accesses the registry. This testcom.dll file is used for both a 32-bit application and a 64-bit application. My requirement is to get the regkey value (path HKEY_LOCAL_MACHINE\SOFTWARE\Test\MyParameters and the key name is Age ). This Age key will be in the 32-bit registry on 32-bit machines and the 64-bit registry (not WOW64) on 64-bit machines.

On a 64-bit machine, when a 32-bit application uses testcom.dll, the Age key is searched in the WOW64 registry. When a 64-bit application uses testcom.dll, the age key is searched in the 64-bit registry.

My requirement is to read the key in the 64-bit registry on 64-bit machines, regardless of which application uses the testcom.dll file. How can i do this?

+6
64bit 32-bit registry
source share
5 answers

If you can change the target .Net version to v4, you can use the new OpenBaseKey function, for example.

 RegistryKey registryKey; if (Environment.Is64BitOperatingSystem == true) { registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); } else { registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32); } 
+9
source share

I did extensive research on this subject and found that managed code is easy to manage in .NET 4, but before .NET 4 you need to load DLLIMport advapi32.dll.

Here are my studies.

http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

+5
source share

I had a similar problem, and the best answer I found was to return to the Win32 registry functions (for example, RegOpenKeyEx ) and go to the corresponding "Security and registry access rights" , in particular OR'ing the samDesired parameter with KEY_WOW64_64KEY .

It was terrible, and I hope you hear the best answer here.

+4
source share

Blair’s answer regarding returning to the Win32 API and calling the RegOpenKeyEx function internally is the best way to achieve this.

Windows itself will map specific registry locations to logical representations using Registry Redirector and Registry Reflection .

Read more about this process in the following MSDN article:
32-bit and 64-bit application data in the registry

Although the Win32 API is the best way to achieve this, it is possible to "hard-code" the location of the registry that you are trying to obtain, although this is fraught with possible problems (Microsoft itself does not support this method). You can learn more about this in this question:
How to open a WOW64 registry key from a 64-bit .NET application

Ultimately, the Win32 API seems to be the best solution (if not the most elegant) at the moment.
Heath Stewart of Microsoft gives the following answer to this MSDN Social question :

Unfortunately, there is no way to pass these flags to managed registry APIs under the Microsoft.Win32 Namespace. You would have to P / call your own API such as specified in RegCreateKeyEx.

Keep in mind, however, if you need to store data in a 32- or 64-bit registry representation. The registry key The redirector in MSDN has keys that are redirected that you are probably familiar with, and the registry key Reflection has keys in which values ​​are copied between 32-bit and 64-bit keys.

If you really need separate types, you can also consider including a registry reflection of your keys if you want both your 32- and 64-bit applications to exchange at least some registry data. See the Documentation for RegEnableReflectionKey for more details.

+2
source share

In the following code, GetAge() will return your key value or null if the key does not exist.

 [DllImport("Advapi32.dll")] static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out int phkResult); [DllImport("Advapi32.dll")] static extern uint RegCloseKey(int hKey); [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")] public static extern int RegQueryValueEx(int hKey, string lpValueName, int lpReserved, ref uint lpType, System.Text.StringBuilder lpData, ref uint lpcbData); public const int KEY_QUERY_VALUE = 0x0001; public const int KEY_WOW64_64KEY = 0x0100; static public string GetAge() { string EPG_REGKEY = @"SOFTWARE\Test\MyParameters"; UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002; int hkey = 0; try { uint lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, EPG_REGKEY, 0, KEY_QUERY_VALUE | KEY_WOW64_64KEY, out hkey); if (0 != lResult) return null; uint lpType = 0; uint lpcbData = 1024; StringBuilder AgeBuffer = new StringBuilder(1024); RegQueryValueEx(hkey, "Age", 0, ref lpType, AgeBuffer, ref lpcbData); string Age = AgeBuffer.ToString(); return Age; } finally { if (0 != hkey) RegCloseKey(hkey); } } 
+2
source share

All Articles