问题
I am running 64-bit Windows, and I want to create the registry key HKCU\Software\Classes\Wow6432Node\CLSID\{myguid}\InprocServer32
using C#.
What registry key should I tell it to write, so that it will be redirected to the above key? This should also work on 32-bit Windows.
I am compiling my app to target x86.
回答1:
If you are using .net 4 you should make use of the RegistryView enumeration.
Pass RegistryView.Registry32
when you call OpenBaseKey. Use HKCU\Software\Classes\CLSID{myguid}\InprocServer32
as your key and let the redirector do the work.
If you are using an older version of .net then I am afraid you will need to p/invoke the native Win32 API.
If you happen to be targetting x86 then you don't need to do anything. The registry redirector will do the right thing and redirect your registry access to the 32 bit view of the registry. You only need to take the steps outline above from a 64 bit process.
回答2:
64-bit versions of Windows emulate 32-bit functionality through the "Windows on Windows" (WoW) subsystem.
In the case of the registry, they move the 32-bit keys over to a special subkey for compatibility reasons. It will automatically redirect 32-bit registry operations to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node
. More details can be found on the MSDN topic about the Registry Redirector.
You can use the RegistryView enum on RegistryKey.OpenBaseKey
to open the 32-bit view explicitly and access HKCU\Software\Classes\CLSID{myguid}\InprocServer32
directly. This will automatically access the WOW64 node on 64-bit systems and the normal key on 32-bit systems.
回答3:
Since you are targetting x86, simply using HKCU\Software\Classes\CLSID\{myguid}\InprocServer32
will work on all platforms.
回答4:
By default your C# application is compiled using "Any CPU" (this is the default -- it means that your program will run as a x86 exe on x86 machine, and x64 on x64 machines). What you want to do is change the setting to Win32. Now your program will always run as an x86 exe, so it will be automatically redirected by windows the WOW6432Node. When you access the HKCU\Software\Classes\CLSID{myguid}\InprocServer32 on an x64 machine you will be redirected to the desired key.
来源:https://stackoverflow.com/questions/8215889/registry-redirection-on-64-bit-windows