reg.OpenSubKey returns incorrect subkey counts and values compared to registry values

Posted on 2014-01-10
Last Modified: 2016-06-13
I'm trying to programmatically return the ODBC driver list from the registry.  The values and names returned are very different (in total number, values, and language) than the list of values seen using regedit.  How do I get the values returned I am expecting?  NOTE: This is in Windows 8.1 64 bit.

This is the code used:
internal static List<String> GetSystemDriverList()
            var names = new List<string>();
            Microsoft.Win32.RegistryKey reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software");

            if (reg != null)
                reg = reg.OpenSubKey("ODBC");
                if (reg != null)
                    reg = reg.OpenSubKey("ODBCINST.INI");
                    if (reg != null)

                        reg = reg.OpenSubKey("ODBC Drivers");
                        if (reg != null)
                            // Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
                            foreach (string sName in reg.GetValueNames())
                        catch { }

            return names;

These are the actual registry values:
But, this is what is returned (first 14 entries shown in the image -- very different than the actual registry values):
returned ODBC driver values (erroneous)
Question by:temerious
  • 2
  • 2
LVL 21

Accepted Solution

Craig Wagner earned 250 total points
ID: 39772437
On the Build page of your project's properties it is probably set to either Any CPU or x86. Change it to x64 and you should get the results you expect.
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39773459
My understanding is that the registry on 64 bits computers is in 2 parts, and the section that is used maps to the Platform target as hinted by Craig.

If you compile to x64, you end up in the standard HKEY_LOCAL_MACHINE\SOFTWARE\ node of the registry, which is the default for a 64-bit application. Theses are the 64-bit drivers are located, and this is what you see in your screen shot.

If you compile to x86, then you end up in the other section of the registry that has been set (as I understand) to enable 32-bit application to keep working properly on 64-bit computers. The results you get then comes from HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC, where the 32-bit ODBC drivers are located. This is what you get in your code.

 (Wonder what the Wow is for. Might be that the programmer who decided on that name is an avid player of World of Warcraft :-)

If you compile with AnyCPU, then where you end up depends on whether the system will end up working in 32-bits ou 64-bits when the application is launched, with some interference from the Prefer 32-bit build option. As I understand it, it depends on the dlls used by the application. This is usually the best way to compile.

The reason why it works so is that 64-bit applications can have problems using 32-bit dlls, since they use values that might be too big for 32-bit.
LVL 21

Expert Comment

by:Craig Wagner
ID: 39773768
FWIW, WoW64 stands for Windows on Windows 64-bit (or Windows on 64-bit Windows).
LVL 40
ID: 39773784
Thanks Craig. But want an awful expression.

I was thinking that now that the compiler is a lot slower (compared to previous versions of Visual Studio), maybe someone as Microsoft was playing WoW while he waited for Windows 8 64-bit to compile :-)

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now