64bit系统下操作注册表的注意事项 ZZ

前一篇 注册表重定向

64bit系统下操作注册表的注意事项

 (2010-03-19 17:50:54)
//z 2012-2-9 17:52:04 IS2120@CSDN

   

1、注册表位置

       64bit系统(Windows Server 2008 R2只有64bit系统)的注册表分32 位注册表项和64位注册表项两部分。

       在64bit系统下,通过regedit中查看到指定路径下的注册表项均为64位注册表项,而32位注册表项被重定位到:HKEY_LOCAL_MACHINE\Software\WOW6432Node。

       应用程序操作注册表的时候也分32bit方式和64bit方式。运行于64bit系统下的32bit应用程序默认操作32位注册表项(即被重定向到WOW6432Node下的子项);而64bit应用程序才是操作的直观子项。
//z 2012-2-9 17:52:04 IS2120@CSDN

       比如,同在64bit系统下,使用如下代码访问注册表:

              ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\Sobey\\MPC "), 0, KEY_ALL_ACCESS, &hKey)

       如果应用程序为32bit子系统,那么实际访问的注册表位置为:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Sobey\MPC(使用regedit工具软件对应的位置);

       而如果应用程序为64bit子系统,那么实际访问的注册表位置将会是:HKEY_LOCAL_MACHINE\SOFTWARE\Sobey\MPC(使用regedit工具软件对应的位置)。

 //z 2012-2-9 17:52:04 IS2120@CSDN

       2、程序编写

       编程过程中,可以使用KEY_WOW64_64KEY和KEY_WOW64_32KEY明确的指定操作64位注册表项或者32位注册表项。例如,

       在32bit子系统应用程序中,可以用如下方式明确指定访问64bit注册表项,程序代码:

              ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\Sobey\\MPC "), 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY,&hKey)

              注意关键字KEY_WOW64_64KEY;

       这种方式写入的注册表项将会确切的位于位置:HKEY_LOCAL_MACHINE\SOFTWARE\Sobey\MPC(使用regedit工具软件对应的位置)。

       在64bit子系统应用程序中,可以用如下方式明确指定访问32bit注册表项,程序代码:

              ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\Sobey\\MPC\\Test"), 0, KEY_ALL_ACCESS |KEY_WOW64_32KEY, &hKey)

              注意关键字KEY_WOW64_32KEY

       这种方式写入的注册表项将会确切的位于位置:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Sobey\MPC(使用regedit工具软件对应的位置)。

       3、特别提醒

       上述说明只针对HKEY_LOCAL_MACHINE主键而言,HKEY_CURRENT_USER主键下没有类似情况;当访问HKEY_CURRENT_USER主键时,明确指定KEY_WOW64_64KEY和KEY_WOW64_32KEY标志也没有意义。

         总之,尽可能使用HKEY_CURRENT_USER主键来保存用户配置吧。

       4、参考文档

         如何通过使用 64 位版本 Windows 查看系统注册表

         Accessing anAlternate Registry View

         Registry Redirector in x64 / IA64

KEY_WOW64_64KEYand KEY_WOW64_32KEY

//z 2012-2-9 17:52:04 IS2120@CSDN
This is really about Win32 on Win64, or Windows on Windows, or shortfor WOW.

MSDN has many information about 64 bits Windows here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/64bitwindows.asp).One particular piece information about registry is htere (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win64/win64/registry_redirector.asp).For the most part, there are two registry hives, 64 bit hive and 32bit hive.Applications running under WOW use 32bit hive, and native 64 bitapplications use 64 bit hive.

If an application running under WOW wants to use 64 bit hive, it has tospecify KEY_WOW64_64KEY in RegOpenKeyEx/RegCreateKeyEx. This is documented here(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_key_security_and_access_rights.asp).

If a registry handle hKey is opened with KEY_WOW64_64KEY under WOW, and youwant to open its subkey, you still have to pass KEY_WOW64_64KEY toRegOpenKeyEx. The WOW system does not remember hKey is opened withKEY_WOW64_64KEY. If you don't pass KEY_WOW64_64KEY to RegOpenKeyEx, WOW willtry to open the subkey in 32bit hive, and will likely fail to find the subkeyif the subkey does not exist in 32bit hive.

KEY_WOW64_32KEY is about native 64 bit applications use 32bit hive. And ithas to follow the same rule.

//z 2012-2-9 17:52:04 IS2120@CSDN 

前一篇 注册表重定向



原文地址:https://www.cnblogs.com/IS2120/p/6745945.html