Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registry.SetValue not working for x86

I want to edit a specific value (type REG_SZ) in the registry for both, x64 and x86, but the SetValue method does not change the value for x86. The x64 works fine. This is my code:

RegistryKey regKeySpecific = RegistryKey.OpenBaseKey(
                                   RegistryHive.LocalMachine, RegistryView.Registry32);

RegistryKey registryKey = regKeySpecific.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderDescriptions\\{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}\\PropertyBag", true);

registryKey.SetValue("ThisPCPolicy", "Show", RegistryValueKind.String);

registryKey.Close();

I'm using the RegistryView.Registry32 parameter in the first code line to change the value in x86 registry, but this is not working.

The problem is identified, but not solved. This code changes always the key in the x64 (WOW6432Node) registry:

"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FolderDescriptions\\{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}\\PropertyBag"
like image 449
Struct Avatar asked Jul 17 '16 16:07

Struct


1 Answers

You evidently have a program executing in x86 (32-bit) mode. Windows x64 performs registry redirection for 32-bit applications, so that trying to access

SOFTWARE\Microsoft

will instead access

SOFTWARE\WOW6432Node\Microsoft

The Registry32 flag makes this same redirection available to .NET applications running as x64. It has no effect for you, because the OS already turned on that redirection.

To access SOFTWARE\Microsoft on a 64-bit OS from a 32-bit process, you should use the Registry64 flag which disables the redirection.

Remember (your question has this backwards)

  • SOFTWARE\ is the native registry, 64-bit on a 64-bit OS

  • SOFTWARE\WOW6432Node\ is the 32-bit compatibility registry on a 64-bit OS

WOW64 is not the layer that provides 64-bit support. It is the layer that provides 32-bit application support when the OS is 64-bit. It means "(Support for) Windows (32) On Windows64".

like image 159
Ben Voigt Avatar answered Oct 24 '22 18:10

Ben Voigt