Anton  Prudyus
Anton Prudyus

Reputation: 113

Why is OpenSubKey() returning null on my Windows 10 64-bit system?

I'm developing x86 app to translate self-created "programming language" to assembler. At some point I need to get path to MASM32. I have read a couple of related topics, but they didn't help me, maybe because I'm new to C#. MASM32 is situated here:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MASM32. When I run my program, I always get "Masm32 is not found" message. What should I do? Thanks in advance!

WindowsIdentity ident = WindowsIdentity.GetCurrent();
        WindowsPrincipal myPrincipal = new WindowsPrincipal(ident);
        if (!myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
        {
            string user = Environment.UserDomainName + "\\" + Environment.UserName;
            RegistrySecurity rs = new RegistrySecurity();
            rs.AddAccessRule(new RegistryAccessRule(user,
                        RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.CreateSubKey | RegistryRights.WriteKey | RegistryRights.ChangePermissions | RegistryRights.SetValue,
                        InheritanceFlags.None,
                        PropagationFlags.None,
                        AccessControlType.Deny));
        }
        try
        {
            RegistryKey baseKey = RegistryKey.OpenBaseKey(
            RegistryHive.LocalMachine,
            RegistryView.Registry64);
            RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Wow6432Node\"); 
            PathMasm32 = baseKey.GetValue("MASM32").ToString();
        }
        catch {
            erTxt = "Masm32 is not found";
              }

Upvotes: 0

Views: 1378

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

It is probably not OpenSubKey() that is causing the null reference exception, but the baseKey.GetValue().ToString() call. The baseKey.GetValue() returns null (because in that case you are trying to get a value right under the HKEY_LOCAL_MACHINE root node) and you invoke ToString() on a null reference. Instead of baseKey.GetValue(), you should try key.GetValue(), assuming MASM32 is really a value under HKLM\SOFTWARE\Wow6432Node which is highly unlikely.

key.GetValue("MASM32").ToString();

Security Note: If you are looking for the installation path of MASM32, even though I do not have any expertise on that, they clearly state that The MASM32 SDK is registry safe and writes nothing to the registry.

Thus, MASM32 is probably a KEY not a VALUE, therefore please execute this method and print the output of it, and you will see the key/value pairs registered under the MASM32 key assuming it exists at the registry path HKLM\SOFTWARE\Wow6432Node\MASM32

public static string GetMASM32LocationFromRegistry()
{
    RegistryKey localMachineRegistryKey;
    RegistryKey masm32RegistryKey;
    RegistryView currentRegistryView = RegistryView.Registry64;

    localMachineRegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, currentRegistryView);
    masm32RegistryKey = localMachineRegistryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\MASM32");

    if (masm32RegistryKey == null)
    {
        return @"ERROR: The registry key HKLM\SOFTWARE\Wow6432Node\MASM32 could not be found";
    }

    StringBuilder masm32RegistryKeyValuesBuilder = new StringBuilder("Key/Value pairs for registry key HKLM\\SOFTWARE\\Wow6432Node\\MASM32:\r\n");
    foreach (string masm32RegistryKeyValueName in masm32RegistryKey.GetValueNames())
    {
        masm32RegistryKeyValuesBuilder.AppendFormat
        (
            "Key: [{0}], Value: [{1}], Value Type: [{2}]\r\n",
            masm32RegistryKeyValueName,
            masm32RegistryKey.GetValue(masm32RegistryKeyValueName),
            masm32RegistryKey.GetValueKind(masm32RegistryKeyValueName)
        );
    }

    return masm32RegistryKeyValuesBuilder.ToString();
}

Upvotes: 1

Related Questions