Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading registry in both 64 and 32 bit windows

I am writing an application in 64 bit Windows-7. In registry I have a key to be read from the path:

HKEY_LOCAL_MACHINE\Software\Wow6432Node\XXXX

I am first trying to open the key using the following code:

RegOpenKeyEx(HKEY_LOCAL_MACHINE, Path, 0, KEY_ALL_ACCESS, &hKey) 

and after that I am able to read the values. This works fine on 64 bit Windows but does not work on 32 bit Windows. What should be done to read it on 32 bit Windows ?

like image 718
Sandeep Kumar Avatar asked Nov 28 '25 11:11

Sandeep Kumar


2 Answers

Windows 64 bit system divide registry into two part. One for 32 and another for 64 bit system. I believe you should update your call to following:

RegOpenKeyEx(HKEY_LOCAL_MACHINE, Path, 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, &hKey)
like image 193
Santosh Dhanawade Avatar answered Dec 01 '25 07:12

Santosh Dhanawade


The WOW64 emulator, and thus the Wow6432Node key, does not exist on 32-bit versions of Windows, only on 64-bit Windows. A 32-bit application running on a 64-bit Windows is redirected to HKEY_LOCAL_MACHINE\Software\Wow6432Node\XXXX key when it tries to access HKEY_LOCAL_MACHINE\Software\XXXX.

The correct solution is to always use the normal path without specifying Wow6432Node at all. On 64-bit Windows, use the KEY_WOW64_32KEY flag if you want a 64-bit process to access a 32-bit key, and the KEY_WOW64_64KEY flag if you want a 32-bit process to access a 64-bit key.

In your example, try this instead:

REGSAM Rights = KEY_QUERY_VALUE;
#ifdef _WIN64
Rights |= KEY_WOW64_32KEY;
#endif

RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\XXXX"), 0, Rights, &hKey);

Read the MSDN documentation for more details:

Registry Redirector

Registry Keys Affected by WOW64

Accessing an Alternate Registry View

like image 45
Remy Lebeau Avatar answered Dec 01 '25 07:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!