• 64bit HKLM Registry Gotcha

    by  • August 23, 2012 • .Net, Programming • 0 Comments

    I’ve recently ran into some interesting problems when trying to access the registry on a 64bit machine via C#.

    Now most people should know that writing (and sometimes even reading) from certain sections of the registry results in either an exception or would return null; indicating the registry key is empty or does not exist.

    RegistryKey myKey = Registry.LocalMachine.CreateSubKey
            ("SOFTWARE\\MyCompany\\MyAppName");
    

    The first and common mistakes is that you require Admin rights, and sometimes UAC will step in to certain registry keys. The second is setting the parameter indicating that you wish to write to the registry.

    RegistryKey myKey = Registry.LocalMachine.CreateSubKey
            ("SOFTWARE\\MyCompany\\MyAppName", true);
    

    What got me here was the next quirk, which caused me much confusion. I had two projects, the first was compiled as 32bit and the second as 64bit, both running on a 64bit machine. When I wrote keys out to HKLM via the 32bit software they weren’t showing up in HKLM\SOFTWARE\MyCompany but were successfully being read. What was actually happening was that these requests were being diverted via the Wow6432Node SubKey HKLM\SOFTWARE\Wow6432Node\MyCompany Once I realised this odity it was fine, but caused issues when interacting with the registry via a native 64bit app. If accessing the registry in C# via the Registry class, this translation is done transparently so you don’t need to worry, but C/C++ and other languages which have direct access may have issues (which I did) when trying to find the correct keys.

    About

    Software engineer. Tea drinker

    http://MrPfister.com

    Leave a Reply

    Your email address will not be published. Required fields are marked *