views:

64

answers:

2

I am trying to query the following registry key values:

HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SharedMemoryOn HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib\ProtocolOrder

But depending on which machine I'm running the program the query returns null. When I debug on my local machine and I inspect the value for ValueCount for:

HKLM\SOFTWARE\Microsoft\MSSQLServer\Client HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib

The count is 0 and OpenSubKey returns null.

I am a domain admin, in the local administrators group and have added the following to my app.manifest:

        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Any idea why?

        private static void ValidateSqlClientSettings()
    {
        Console.WriteLine("\r\n/////////////// LOCAL SQL CLIENT PROTOCOLS ////////////////");

        RegistryKey keyHKLM = Registry.LocalMachine;
        ///TODO: nullreferenceexception - connect to remote machine and find out why
        RegistryKey sqlClientKey = keyHKLM.OpenSubKey(@"SOFTWARE\Microsoft\MSSQLServer\Client");

        if (sqlClientKey == null)
        {
            WriteLine2Console(@"WARNING: unable to read registry key '{0}\SOFTWARE\Microsoft\MSSQLServer\Client'", ConsoleColor.Yellow);
        }

        var cliKeyNames = from k in sqlClientKey.GetSubKeyNames()
                          where k == "SuperSocketNetLib"
                          select k;

        ///TODO: find out why these values are always missing (even if I can see them in regedit)
        Console.Write("Shared Memory Disabled (cliconfg): ");
        if (Convert.ToBoolean(sqlClientKey.GetValue("SharedMemoryOn")))
            WriteLine2Console("FAILED", ConsoleColor.Red);
        else if(sqlClientKey.GetValue("SharedMemoryOn") == null)
            WriteLine2Console(String.Format("WARNING - unable to read '{0}\\SharedMemoryOn'", sqlClientKey.Name), ConsoleColor.Yellow);
        else
            WriteLine2Console("PASS", ConsoleColor.Green);

        Console.Write("Client Protocol Order (cliconfg - tcp first): ");
        foreach (string cliKey in cliKeyNames)
        {
            RegistryKey subKey = sqlClientKey.OpenSubKey(cliKey);
            object order = subKey.GetValue("ProtocolOrder");
            if (order != null && order.ToString().StartsWith("tcp") == false)
            {
                WriteLine2Console("FAILED", ConsoleColor.Red);
            }
            else if (order == null)
            {
                WriteLine2Console(String.Format("WARNING - unable to read '{0}\\ProtocolOrder'", subKey.Name), ConsoleColor.Yellow);
            }
            else
            {
                WriteLine2Console("PASS", ConsoleColor.Green);
            }
            subKey.Close();
        }

        sqlClientKey.Close();
        keyHKLM.Close();
    }
A: 

Try changing your sqlclientkey to the following:

RegistryKey sqlClientKey = keyHKLM.OpenSubKey(@"SOFTWARE\\Microsoft\\MSSQLServer\\Client"); 
Darxval
A: 

Another factor you should pay attention to is application bitness.

On Windows x64, your x86 build will look for the registry keys under (in regedit) "SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client", when you specify (in code) "Software\Microsoft\MSSQLServer\Client"

That's a WOW64 redirection for registry keys.

As Visual Studio 2010 by default creates exe in x86 mode, you should pay attention to this tip.

Lex Li
Thanks! I changed my platform target to "Any CPU" and it worked. I haven't tried the result on an x86 machine though so that may also be important to verify. But I hadn't noticed the x86 default before.
Mark J Miller