Click here to Skip to main content
15,885,366 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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Sep-13 0:39am    
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...
Sergey Alexandrovich Kryukov 27-Sep-13 3:06am    
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
 
Share this answer
 
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\"
Sergey Alexandrovich Kryukov 26-Sep-13 2:29am    
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
Sergey Alexandrovich Kryukov 26-Sep-13 2:47am    
Please see the updated answer, after EDIT. (And thank you for your clarifications.)
—SA
Sergey Alexandrovich Kryukov 26-Sep-13 2:57am    
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 .

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