Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing to x86 and x64 registry keys from the same application

I am running my application compiled as x86, and it is running on 64 bit Windows.

In order to fix a problem with ClickOnce file associations I want to read some CLSID values from the x86 view of the registry and then write them to the x64 view.

To be clear, this means that from an x86 application I want to simultaneously read from the x86 registry view and then write to the x64 registry view. I want to take the values I find under HKEY_CURRENT_USER\Software\Classes\CLSID\{my clsid} and write them to HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{my clsid}.

How should I do this? Using a RegistryView is producing unexpected results. For example, this OpenSubKey call returns null:

keyPath = @"Software\Classes\CLSID\" + clsid;
var regularx86View = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
var regularClassKey = regularx86View.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree);

If I use RegistryView.RegistryDefault or RegistryView.Registry64 instead it returns the key - but I would expect it to return null when using Registry64 because that key doesn't exist in the x64 view and there should be no redirection taking place.

Is using a RegistryView the appropriate thing to be doing, or should I be using the WinAPI directly?

like image 742
slugster Avatar asked Mar 15 '13 03:03

slugster


1 Answers

I might be misunderstanding what you're asking but if you're running in a 32bit process then all your keys will be in the Wow6432Node\xxxxx node anyway. So if you tried to copy them from HKEY_CURRENT_USER\Software\Classes\CLSID\{my clsid} (and didn't specify the 64 bit view manually) to HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{my clsid} you would be copying the same values. This code should work:

keyPath = @"Software\Classes\CLSID\" + clsid;
var regularx86View = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
// Note this calls HKEY_CURRENT_USER\Software\Classes\Wow6432Node\CLSID\{my clsid}:
var regularClassKey = regularx86View.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree); 

var regularx64View = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
// Note this calls HKEY_CURRENT_USER\Software\Classes\CLSID\{my clsid}:
var regularClassKey = regularx64View.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree); 
like image 179
Zipper Avatar answered Sep 17 '22 13:09

Zipper