How to open a WOW64 registry key from a 64-bit .NET application

后端 未结 4 848
萌比男神i
萌比男神i 2020-11-29 04:54

My .NET application (any-CPU) needs to read a registry value created by a 32-bit program. On 64-bit Windows this goes under the Wow6432Node key in the registry. I have read

相关标签:
4条回答
  • 2020-11-29 05:03

    In the case where you explicitly need to read a value written by a 32 bit program in a 64 bit program, it's OK to hard code it. Simply because there really is no other option.

    I would of course abstract it out to a helper function. For example

    public RegistryKey GetSoftwareRoot() {
      var path = 8 == IntPtr.Size 
        ? @"Software\Wow6432Node"
        : @"Software";
      return Registry.CurrentUser.OpenSubKey(path);
    }
    
    0 讨论(0)
  • 2020-11-29 05:10

    If you can change the target .Net version to v4, then you can use the new OpenBaseKey function e.g.

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 05:23

    Extending Anders's answer, there's a good example of wrapping the resulting handle in a .NET RegistryKey object on Shahar Prish's blog - be sure to read the comments too though.

    Note that unvarnished use of the pinvoke.net wrapper of RegOpenKeyEx is fraught with issues.

    0 讨论(0)
  • 2020-11-29 05:24

    The correct way would be to call the native registry api and passing the KEY_WOW64_32KEY flag to RegOpenKeyEx/RegCreateKeyEx

    0 讨论(0)
提交回复
热议问题