Question

I'm having hard time with access the registry in my program (C#), here is the details :

  • My program run on 32 bit & 64 bit OS.
  • My program will need to get registry keys of software that some has 64 version and some not.

For example:

I'm running a 64 bit OS and i need to read 2 registry keys so i can know the install locations of the 2 different software, one has a 64 bit version so i don't need the "\Wow64node\" and one have only has 32 bit version so i will need the "\Wow64node\"

// 1.MSE: (64 bit version)
       string installPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Antimalware", "InstallLocation", null);
       if (installPath != null)
       {
           listViewAV.Items.Add("MSE");
       }
// 2.Avast: (32 bit version)
       installPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\AVAST Software\Avast", "ProgramFolder", null);
       if (installPath != null)
       {
           listViewAV.Items.Add("Avast");
       }

Notice that the Avast is in the Wow Node and MSE isnt, but if my program will be running on a 32 bit OS they both be without the Wow Node.

How can i write this code so it works on every scenario?

Scenario 1 : running on a 32 bit machine so all the softwares will be 32 bit as well(no "Wow64node").

Scenario 2 : running on a 64 bit machine so some of the softwares will be 32 bit and some and some 64 bit(need to handle with "Wow64node")

Please write a detailed answer(prefer with a code example). Thank you

Was it helpful?

Solution

This is essentially the same question as you asked earlier today, and that I answered. I won't repeat my answer. What you are asking for is some code. I can give you that, but I fear that you still don't really understand the registry redirector. I think you are going to have to get on top of that before you can expect to make any progress.

To read a value from the 32 bit view of the registry, for instance Avast, use this code:

RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
    RegistryView.Registry32);
rk = rk.OpenSubKey(@"SOFTWARE\AVAST Software\Avast");
string installPath = null;
if (rk != null)
{
    installPath = rk.GetValue("ProgramFolder", null);
}
if (installPath != null)
{
    .... // Avast registry setting found
}

For a program registered in the 64 bit view of the registry, for instance MSE, do it like this:

RegistryKey rk = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
    RegistryView.Registry64);
rk = rk.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft Antimalware");
string installPath = null;
if (rk != null)
{
    installPath = rk.GetValue("InstallLocation", null);
}
if (installPath != null)
{
    .... // Avast registry setting found
}

You don't need to worry about whether or not you are running on a 32 bit or 64 bit system. On a 64 bit system, both views exist, and you get the view that you request. On a 32 bit system, only the 32 bit view exists, and the value of the RegistryView enumeration that you pass is ignored. The documentation says it like this:

If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top