Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

c# - 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_USERSoftwareClassesCLSID{my clsid} and write them to HKEY_CURRENT_USERSoftwareClassesWow6432NodeCLSID{my clsid}.

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

keyPath = @"SoftwareClassesCLSID" + 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Wow6432Nodexxxxx node anyway. So if you tried to copy them from HKEY_CURRENT_USERSoftwareClassesCLSID{my clsid} (and didn't specify the 64 bit view manually) to HKEY_CURRENT_USERSoftwareClassesWow6432NodeCLSID{my clsid} you would be copying the same values. This code should work:

keyPath = @"SoftwareClassesCLSID" + clsid;
var regularx86View = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
// Note this calls HKEY_CURRENT_USERSoftwareClassesWow6432NodeCLSID{my clsid}:
var regularClassKey = regularx86View.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree); 

var regularx64View = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
// Note this calls HKEY_CURRENT_USERSoftwareClassesCLSID{my clsid}:
var regularClassKey = regularx64View.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadSubTree); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...