为什么我不能在64位机上正确读取HKCU的32位注册表值?

时间:2022-07-18 07:23:37

I'm stumped on a windows 7 registry problem and while various questions and answers get me some of the way there, nothings I've seen addresses my particular issue. I don't know if other windows versions affects this problem, but we all have win7x64 machines.

我被windows 7注册表的问题难住了,虽然各种各样的问题和答案让我找到了一些解决办法,但我所见过的没有什么能解决我的问题。我不知道其他windows版本是否会影响这个问题,但我们都有win7x64机器。

We have a wide variety of tools at our work, some C++, some C#, some python (2.6), etc. We also run a mix of 32 and 64 bit tools. In the past, we've happily stored registry information in HKLM. We've been working on moving stuff into HKCU. We've had a lot of discussions about whether to do this, affects on UAC, etc. We really want to try and make this move. That said:

我们的工作中有各种各样的工具,一些c++、一些c#、一些python(2.6)等等。在过去,我们很高兴地将注册表信息存储在HKLM中。我们一直在努力把东西搬到香港中文大学。我们已经讨论了很多关于是否这样做,对UAC的影响,等等。我们真的很想尝试一下。也就是说:

We are having trouble reading/writing registry keys out of HKCU/software/CompanyABC/App. We have a setup app write in python that writes out registry keys to the above location using _winreg. Whether or not we specify KEY_WRITE|KEY_WOW64_32KEY or just KEY_WRITE, the values get writte to HKCU/Software/WOW6432Node/companyABC/app. Fine.

我们在使用HKCU/software/CompanyABC/App阅读/书写注册表时遇到困难。我们有一个用python编写的安装应用程序,它使用_winreg将注册表键写到上面的位置。无论我们是否指定KEY_WRITE|KEY_WOW64_32KEY还是只是KEY_WRITE,这些值都被写入HKCU/Software/WOW6432Node/companyABC/app。很好。

Then I have a C# app that tries to read these values. Using Microsoft.Win32.Registry, I open the subkey ('HKCU/Software/CompanyABC/app') and I don't see my values. Turns out that I'm seeing the following behavior:

然后我有一个c#应用试图读取这些值。使用Microsoft.Win32。我打开子密钥(HKCU/Software/CompanyABC/app),我看不到我的价值。我发现我有以下行为:

  • When reading/writing registry keys from HKLM, this stuff all just works. The python app will write to HKLM/Softare/Wow6432Node/CompanyABC/app, and the C# code will read from that location. This all makes proper sense too, given how we are building our C# apps, and writing the registry values via python
  • 当从HKLM读取/写入注册表项时,这些东西都可以工作。python应用程序将写入HKLM/Softare/ wow6432节点/CompanyABC/应用程序,c#代码将从该位置读取。考虑到我们如何构建c#应用程序,以及如何通过python编写注册表值,这一切也都是合理的
  • Reading/writing the registry values from HKCU, I get different behavior. The _winreg functions will write to HKCU/Sofrware/Wow6432Node/CompanyABC/app, but the C# app will read from HKCU/Software/CompanyABC/app. The C# app is built as an x86 app (not Any CPU and not x64) so I assumed that the app would get properly redirected to the wow6432Node, but it doesn't seem to.
  • 读/写HKCU的注册表值,我有不同的行为。_winreg功能将写入HKCU/Sofrware/Wow6432Node/CompanyABC/app,但c#应用将读取HKCU/Software/CompanyABC/app。c#应用程序是作为一个x86应用程序构建的(不是任何CPU,也不是x64),所以我假设这个应用程序会被正确地重定向到wow6432节点,但它似乎不是。

after some investigation, it appears that HKCU/Software is different. This article seems to indicate that this area is "shared" and not redirected. If that is the case, then I cannot understand why our python app (again, using _winreg) is writing to a location in HKCU that uses the Wow6432Node - it seems like it should be writing it without that redirection in place. I suppose that it could be a bug in _winreg.

经过一番调查,似乎HKCU/软件是不同的。这篇文章似乎表明这个区域是“共享的”而不是重定向的。如果是这样的话,那么我就无法理解为什么我们的python应用程序(再次使用_winreg)要写到HKCU中使用wow6432节点的位置——似乎它应该在没有重定向的情况下写它。我认为它可能是_winreg中的一个bug。

I really want to avoid tacking on WOW6432Node explicitly in our tools, but that is where I am at today. Can anyone explain to me how I can make registry accesses from 32 and 64 bit processes into HKCU work correctly without having to resort to hard coded paths into the 32bit hive?

我确实想避免在我们的工具中显式地加载wow6432节点,但这就是我今天的目标。有谁能向我解释一下,我怎样才能使注册表访问从32位和64位进程正确地进入HKCU,而不需要使用硬编码路径进入32位的hive?

1 个解决方案

#1


4  

I understand from the comments on the question that this issue has gone away, for anyone else that encounters this question, you can use Microsoft.Win32.OpenBaseKey to specify whether to open the 64 or 32-bit portion of the registry when running on a 64-bit machine even if your process is running as a 32-bit process.

我从对这个问题的评论中了解到这个问题已经消失了,对于任何遇到这个问题的人来说,你可以使用Microsoft.Win32。OpenBaseKey指定在64位机器上运行时是否打开注册表的64位或32位部分,即使您的进程是作为32位进程运行的。

If you always want to access the keys in the NON-WOW6432Node section of the registry, then you can safely set the View parameter of OpenBaseKey to RegistryView.Registry64. This will work correctly on both 64 and 32-bit OSes.

如果您总是希望访问注册中心的非wow6432node部分中的键,那么可以将OpenBaseKey的视图参数安全地设置为RegistryView.Registry64。这将在64位和32位操作系统上正确工作。

#1


4  

I understand from the comments on the question that this issue has gone away, for anyone else that encounters this question, you can use Microsoft.Win32.OpenBaseKey to specify whether to open the 64 or 32-bit portion of the registry when running on a 64-bit machine even if your process is running as a 32-bit process.

我从对这个问题的评论中了解到这个问题已经消失了,对于任何遇到这个问题的人来说,你可以使用Microsoft.Win32。OpenBaseKey指定在64位机器上运行时是否打开注册表的64位或32位部分,即使您的进程是作为32位进程运行的。

If you always want to access the keys in the NON-WOW6432Node section of the registry, then you can safely set the View parameter of OpenBaseKey to RegistryView.Registry64. This will work correctly on both 64 and 32-bit OSes.

如果您总是希望访问注册中心的非wow6432node部分中的键,那么可以将OpenBaseKey的视图参数安全地设置为RegistryView.Registry64。这将在64位和32位操作系统上正确工作。