Click here to Skip to main content
14,983,711 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I have used the following code in c# to read registry value --
C#
RegistryKey regkey;
            regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);


my environment is as --

OS --- windows server 2008-R5 (64 bit)
visual studio 2008
and in project property platform target is "x86 "

My requirement is that i want to write a code which should work with platform target as "x86 ".
when same code i used and changed platform target x64 or any cpu , it worked fine .
but i need platform target as x86 only .

i google it and found that we need to use @"SOFTWARE\Wow6432Node\" but even it did not resolve my issue .

So please help me regarding this , Thanks in advance .
Posted
Updated 6-Feb-19 2:51am

Be careful about the redirection on 64bit operating systems. When you do
C#
string path = @"Software\Microsoft\Office"
RegKey regkey = Registry.LocalMachine.OpenSubKey(path);

on a 64bit system with a 32bit application, the key opened by the lines above is actually
HKLM\Software\Wow6432Node\Microsoft\Office
- note the Wow6432Node in the path! Hence take care when you look into the registry with regedit.
   
Comments
   
5ed, but OP remains confused. Please see our discussion on the x86 view under WoW64 in the comments to my answer.
Could you explain to OP that the picture looks consistent, you tell your opinion of the problem. OP is preoccupied with the idea of "correct values", while in x86 view the value (more exactly, absence of some sub-key) is also "correct", this is what the registry really has...
—SA
Bernhard Hiller 27-Sep-13 2:43am
   
Thanks. Meanwhile, the OP has accepted your answer, and perhaps understood the problem.
The x86/x64 issues can be very confusing. But there are also two regedit-versions available: he could open it from C:\Windows\SysWOW64 and find out that there is no Wow6432Node, and when comparing with the "normal" regedit, he could see that it actually shows the Wow6432Node...
   
Oh, thank for the note.
Good idea, by the way, I did not know about WoW64 regedit, thank you. With .NET 3.5 (where Views were not yet directly supported by the API), it can be very helpful.
—SA
It simply means that the search by key failed: http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx[^].

The reason is simple: the key path is not found. Use Regedit to check it up. I also can tell you that the problem is not related to the lack of required privilege: in such case, the call would throw an exception.

[EDIT #1]

Please see my comment below.

Here is your problem: you are working with x86 instruction-set architecture via WoW64 on one of the 64-but. Your registry has separate different views per architecture, so, by default, you are using the view Microsoft.Win32.RegistryView.Registry32. If you are checking the path with Regedit, you may see different data, because you are looking at RegistryView.Registry64.

So, if you need to access some registry data in one view, you should decide which one. Then, in general case, you should specify the view explicitly:

C#
RegistryKey baseKey = RegistryKey.OpenBaseKey(
     RegistryHive.LocalMachine,
     RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(yourPath, RegistryKeyPermissionCheck.ReadSubTree); 


Alternatively, you can work with different data sets for different views/architectures.

Are you getting the picture?

[EDIT #2]

Besides, you may need to have different code for different target instruction-set architectures. For example, on the 32-bit system, you may need to use Default key.

First, you need to inquire the "bitness" of the current process. For this, just get the size of the IntPtr:
http://msdn.microsoft.com/en-us/library/system.intptr.size.aspx[^].

The remaining option, if this is 4 (32-bits), is where it is being executed, on WoW64 or native platform. This is how:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx[^].

Apply some logic. Note, that you don't have to determine exact 64-bit architecture for your purpose (x86-64 or Itanium), which would be more difficult.

See also:
http://en.wikipedia.org/wiki/WOW64[^],
http://en.wikipedia.org/wiki/X86[^],
http://en.wikipedia.org/wiki/X86-64[^],
http://en.wikipedia.org/wiki/Itanium[^].

—SA
   
v6
Comments
lalit.mca2006 26-Sep-13 0:42am
   
thanks sergery to reply but path is correct because its working in platform target 64 or "ANY CPU".

full path was :- "SOFTWARE\mycomanayname\Settings\ActiveDirectory\"
   
How do you know this path actually exist? (Is it "mycomanayname" really? strange name...) Do you understand that, to use this call, the whole path should exist prior the call? Did you check it up? And you need to open the hive of RegistryView32, as x86 target architecture works via WoW64 on each of 64-bit architectures.
—SA
   
Please see the updated answer, after EDIT. (And thank you for your clarifications.)
—SA
   
And now [EDIT #2]...
—SA
lalit.mca2006 26-Sep-13 7:26am
   
Thanks for such valuable information but sergery still i want one more favor because i am using .net framework 3.5 and in 3.5 ,i am not able to find RegistryKey.OpenBaseKey(....) method .
   
Oop! I did not pay attention, sorry. Indeed, this method is absent from 3.5.
Probably you should use Wow6432Node instead...
—SA
lalit.mca2006 26-Sep-13 22:46pm
   
Yes SA , i tried but still no luck . So could you please suggest me any other way to do the same .
Sergey Alexandrovich Kryukov 26-Sep-13 23:14pm
   
Let's go again. You can compile your code for x86, as well as x86-64 (x64), right?

It looks like equivalent paths should be:

for x86 code:
"SOFTWARE\..."

for x64 code:
"SOFTWARE\Wow6432Node\..."

Maybe you are trying to do the opposite? Please understand: you access x86 (32-bit) view from x64 (x86-64), not visa versa.

—SA
lalit.mca2006 27-Sep-13 0:10am
   
Let me explain once again :-
i have 64 bit machine windows server -2008 and my project is a windows(desktop) application in .net framework 3.5 .Now i want to read some registry values .

these are the following outputs which am getting :-
path =SOFTWARE\xxx\Settings\ActiveDirectory\
RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);
In platform target x86 regkey = null
platform target x64 & any cpu = getting correct value
---------------------------------------------------------

path =SOFTWARE\Wow6432Node\xxx\Settings\ActiveDirectory\
RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);
if platform target x86 , regkey = null
if platform target x64 & any cpu regkey== null


SA , i have limitation that i should use only platform target as x86 , so if i use x86 then what should i do to get registry value .
please let me know whether i am able to explain my actual issue .
   
This is exactly what I meant from the very beginning. Everything looks consistent.
You have nothing on this key in a x86 view, that's it. What's the problem? Either don't use this key, or add something there, to make it the same as in x64 view. Do you see the picture?

When you are trying to compare this fact with what you see using Regedit, you get confused, but only because this application is compiled to x64 target. That's why the node Wow6432Node is supported, to map what you would see in x86 view.

If the views show different results, they are... just different. Either make them the same, or leave with it. Everything is consistent.

—SA
lalit.mca2006 27-Sep-13 0:47am
   
thanks SA for this conclusion , i got so many things cleared about my results output .
But please last question is that if i build the project in x86 , should i able to get key value in platform target x86 ?

finally i need to get the registry in x86 . :(
   
I don't quite understand. You will access all the registry, but only in its x86 view, Registry32. This registry should be consistent with that of the 32-bit system, but the code could be executed on all 32-bit and 64-bit platforms in the same way. This is because it will work either on native platform or WoW64, which is a 32-bit subsystem on the OS versions based on x86-64 or IE64 architectures...

By the way, will you accept the answer formally after all (green "Accept" button)?

—SA
lalit.mca2006 27-Sep-13 1:00am
   
Thanks SA , sorry i did not check that feature (green "Accept" button) .
Anyway its good to see your reply .
   
No problem at all...
—SA
lalit.mca2006 27-Sep-13 1:02am
   
i didnot see any picture SA .
   
And I think by now it becomes obvious...
—SA
lalit.mca2006 27-Sep-13 1:45am
   
yes its obvious but need to find that how can i get x64 value when target platform is x86 ?
   
In your case, there is no value, the key is absent in Registry32 view. Leave with it.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900