有一个读取注册表信息的程序 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0, KEY_READ, &hKey) == ERROR_SUCCESS)/
,在32位下完全正常,但是在64位返回值正确,但就是读不到东西。后来单步发现读不到东西,就搜64位读注册表失败,发现需要加
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0,KEY_READ|KEY_WOW64_64KEY, &hKey) == ERROR_SUCCESS)就可以了,我是全部把权限提高,还可以根据不同的操作系统,设置不同的参数。
IsWow64Process 判断64位操作系统
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
IsWow64返回TRUE则是64位系统,否则为32位系统。
BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
return FALSE;
}
}
return bIsWow64;
}
可参考的文献:
http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx
http://www.codeproject.com/Articles/51326/Net-Compilation-registry-accessing-and-applicatio
http://boluns.blog.163.com/blog/static/69845968201071132032313/
The code
The next code is a personal translation to C# of several pieces of code I found, mostly from here:
Hide Shrink Copy Code
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int sam,
out IntPtr phkResult);
[Flags]
public enum eRegWow64Options : int
{
None = 0x0000,
KEY_WOW64_64KEY = 0x0100,
KEY_WOW64_32KEY = 0x0200,
How to use the Code
The OpenSubKey will return the searched key, allowing you to specify reading from the normal registry, or from the alternative 32-bit, WOW64 registry. The following example reads from the 32-bit WOW64 registry:
Hide Copy Code
try
{
RegistryKey key = OpenSubKey(Registry.LocalMachine,"Software\\[Key]",false,
eRegWow64Options.KEY_WOW64_32KEY);
}
catch
{
You just need to place your key name where “[Key]” is.