mehta ankit
mehta ankit

Reputation: 43

64 bit Registry Key not updated by 32 bit application

Registry Key is not updated at mentioned path in C#

string path = @"Software\Microsoft\Windows NT\CurrentVersion\Windows\"; 
RegistryKey myKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, true);
myKey.SetValue("USERProcessHandleQuota", 50000, RegistryValueKind.DWord); 
myKey.Close();

instead of given path value update at below path :-

@"Software\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Windows\";

My machine is 64 bit and the application I am running is 32 bit.

Kindly suggest how to update value at above path address i.e in Software\Microsoft and not Software\wow6432node.

Upvotes: 2

Views: 490

Answers (1)

TheGeneral
TheGeneral

Reputation: 81493

You can do this 2 ways

  • RegistryView.Registry64 and OpenBaseKey in .Net 4 and above
  • RegOpenKeyEx Api call in Advapi32.dll with the KEY_WOW64_64KEY flag in earlier versions

RegistryView Enumeration : On the 64-bit version of Windows, portions of the registry are stored separately for 32-bit and 64-bit applications. There is a 32-bit view for 32-bit applications and a 64-bit view for 64-bit applications.

You can specify a registry view when you use the OpenBaseKey and OpenRemoteBaseKey(RegistryHive, String, RegistryView) methods, and the FromHandle property on a RegistryKey object.

Code using Registry64

using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
   using (var subKey = baseKey.OpenSubKey("blah", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
   {
      if (subKey != null)
      {
         var value =  subKey.GetValue("Somekey");
      }
   }
}

PInvoke using RegOpenKeyEx and KEY_WOW64_64KEY can be found here

PInvoke RegOpenKeyEx (advapi32)

Note : Both these should allow your 32 bit app to access the full registry, not just the Wow6432Node registry redirector.

Resources

RegistryKey.OpenBaseKey Method (RegistryHive, RegistryView)

RegistryKey.OpenSubKey Method (String, RegistryKeyPermissionCheck, RegistryRights)

RegistryView Enumeration

Registry Key Security and Access Rights

32-bit and 64-bit Application Data in the Registry

Upvotes: 5

Related Questions