11

I was wondering if it was possible to access the following registry key in C# on a 64 bit pc.

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

When accessing on a 32bit pc it works fine but on 64 it re - directs it to the native 64bit path of HKLM\SOFTWARE\Wow6432Node which has different keys. I have looked through various articles on this but can't really find a definite answer on how to access the 32bit key in a 64bit pc in C#. Thanks.

Bali C
  • 30,582
  • 35
  • 123
  • 152

5 Answers5

15

If you are targetting the .Net Framework version 4.0 or above then you can do this by passing the RegistryView.Registry32 value when opening the desired registry key.

If you are targetting a previous version of the .Net Framework then you need to use P/Invoke to call RegOpenKeyEx directly yourself allowing you to pass the KEY_WOW64_32KEY flag.

There is a guide here that goes into more detail:

Justin
  • 84,773
  • 49
  • 224
  • 367
5

Compile your application in x64, and all should be well. In Visual Studio 2010, you'd do that by changing the setting under Project Properties > Build

enter image description here

For VS Express users:

In VC# Express, this property is missing, but you can still create an x86 configuration if you know where to look.

It looks like a long list of steps, but once you know where these things are it's a lot easier. Anyone who only has VC# Express will probably find this useful. Once you know about Configuration Manager, it'll be much more intuitive the next time.

  1. In VC# Express, go to Tools -> Options.
  2. In the bottom-left corner of the Options dialog, check the box that says, "Show all settings".
  3. In the tree-view on the left hand side, select "Projects and Solutions".
  4. In the options on the right, check the box that says, "Show advanced build configuraions."
  5. Click OK.
  6. Go to Build -> Configuration Manager...
  7. In the Platform column next to your project, click the combobox and select "".
  8. In the "New platform" setting, choose "x64".
  9. Click OK.
  10. Click Close.
  11. There, now you have an x64 configuration! Easy as pie! :-)
foxy
  • 7,599
  • 2
  • 30
  • 34
  • Thanks, is that only for Visual Studio Pro, I am using Express and can't find that option? – Bali C Aug 05 '11 at 12:10
  • @Bali, I have edited my answer with instructions that will *hopefully* work, adapted from http://forums.create.msdn.com/forums/t/4377.aspx#22601 . Please go ahead and try it out :) – foxy Aug 05 '11 at 12:15
  • Although you've accepted this answer, you must realise that creating a 64 bit executable will mean that your app won't run on 32 bit Windows. Is that what you want? – David Heffernan Aug 05 '11 at 12:51
  • Hmmm, no I didn't realise that, I will keep this as the accepted answer though as I might create separate 32 and 64 bit apps, thanks for pointing that out – Bali C Aug 05 '11 at 13:03
5

Project + Properties, Build tab, Platform target = Any CPU. Accessing the 64-bit registry from a 32-bit app requires .NET 4 and the new RegistryKey.OpenBaseKey() method with the RegistryView.Registry64 option.

This only lets you read the key, writing the key values requires UAC elevation. You can write to HKCU without elevation and without being subjected to registry redirection.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

You have that backwards, the Wow6432Node is for 32-bit applications. So if your application si 32-bit (x86) then you would automatically be redirected to that "node".

You can use the FromHandle method in .NET 4 to specify which view to use, but it's use is not very obvious and there can be problems.

This answer addresses this question using the Win32 API, which you can leverage in C# as well.

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
1

Its suppose to redirect. You need to detect this redirection and read the WoW6432Node key instead. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run is the 64-bit registry which can only be accessed by 64-bit applications.

Obviously you shoudl write code to support both.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • @David - I am going to have to disagree with your comment. The redirection to HKLM\SOFTWARE\Wow6432Node is unknown to a 32-bit application. If you feel you can add clarification to my answer I would appreciate it ( instead of making a baseless claim that I am incorrect ). – Security Hound Aug 05 '11 at 12:45
  • You are right that I am unclear. Sorry. `HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run` is a meaningless key. The shell reads the values in `HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run`. It's possible that it reads values in the 32 bit area as a sop to programs that get it wrong, but the correct place to write values in the 64 bit section. – David Heffernan Aug 05 '11 at 12:47
  • @David - I do not believe you are right. A 32-bit process would not write to keys that are meant for 64-bit operating systems. A 32-bit program attempting to write/read to HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run would be redirected to HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run but they are not 100% the same so you have to actually read HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run if your dealing with a 32-bit program. – Security Hound Aug 05 '11 at 16:17
  • The application that owns and reads this key is the shell which is a 64 bit process. – David Heffernan Aug 05 '11 at 16:47
  • Actually I think that in this case, both 32 and 64 bit versions of the `CurrentVersion\Run` lists are processed. So in fact it doesn't actually matter which you write to. I'm assuming that OP wants to write to this key. But the principle is the same. – David Heffernan Aug 05 '11 at 18:56