Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I properly read 32bit registry values in HKCU on 64bit machine?

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.

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:

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 written to HKCU/Software/WOW6432Node/companyABC/app. Fine.

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:

  • When reading/writing registry keys from HKLM, this stuff all just works. The python app will write toHKLM/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
  • Reading/writing the registry values from HKCU, I get different behavior. The _winreg functions will write toHKCU/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.

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.

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 32-bit hive?

like image 592
Mark Avatar asked Nov 21 '11 18:11

Mark


1 Answers

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.

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.

like image 136
competent_tech Avatar answered Sep 19 '22 12:09

competent_tech