Sandeep Kumar
Sandeep Kumar

Reputation: 105

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 ?

Upvotes: 3

Views: 4690

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595827

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

Upvotes: 3

Santosh
Santosh

Reputation: 1815

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)

Upvotes: 5

Related Questions