Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegistryKey ValueCount/SubKeyCount wrong

Tags:

c#

registry

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();
    }
like image 635
Mark J Miller Avatar asked Aug 24 '26 11:08

Mark J Miller


1 Answers

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.

like image 86
Lex Li Avatar answered Aug 27 '26 00:08

Lex Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!