Home
Archives

64-bit registry redirection coding in C++

There are a few things about accessing the windows registry on a 64-bit machine that might be a little tricky for someone just getting into it.  The first thing to realize is that on a 64-bit operating system, MSFT created a separate hive under HKLM\Software (Wow6432Node) for backwards compatability.  Whenever a 32-bit application is running on a 64-bit machine, any registry reads or writes will be automatically forwarded to this node.



As mentioned before, this was done by MSFT for backward compatability reasons and to allow both a 32-bit and 64-bit application to run on the same machine.  What can happen though, as in my case, is that 64-bit components and 32-bit components of the same app get redirected to different hives, causing problems where a needed registry key can't be found.

To overcome this issue, I use the KEY_WOW64_64KEY flag when accessing the registry to ensure that my app won't get redirected to the Wow6432Node. 

This would look like:

RegCreateKeyEx(myKey, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, 
               (KEY_WOW64_64KEY | KEY_ALL_ACCESS), NULL, &hKey, NULL );


Note that in the 6th parameter I've OR'd the KEY_WOW64_64KEY flag.

This can cause issues though, especially if you use this flag on a Window 2k system.  DON'T DO THIS!  It will cause unknown errors to happen.  The best way to handle this is to check to make sure the OS is newer than Windows 2k as so:

DWORD dwFlag = 0 ;

if(VerifyMyOSVersion())
     dwFlag = KEY_WOW64_64KEY;
     
RegCreateKeyEx(myKey, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, 
               (dwFlag | KEY_ALL_ACCESS), NULL, &hKey, NULL );