Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / C#
Technical Blog

Access 64-bit HKLM\Software Registry by 32-bit C#.NET Application

Rate me:
Please Sign up or sign in to vote.
4.55/5 (4 votes)
22 Jun 2015CPOL 24.2K   3  
Access 64-bit HKLM\Software registry by 32-bit C#.NET application

Introduction

While running 32-bit Windows application on a 64-bit windows OS, there is a registry redirection. Here, if 32-bit application tries to read a key under HKLM\Software, then due to Registry redirection effective path becomes HKLM\Software\Wow6432Node. For example, we are running 64-bit and 32-bit application to read registry keys as HKLM\Software\xyz and HKLM\Software\Wow6432Node\xyz.

So by default, with an input of HKLM\Software, 64-bit application will read HKLM\Software\xyz while because of registry redirection 32-bit application will read HKLM\Software\Wow6432Node\xyz.

In C#, to read 64-bit HKLM\Software registry keys, we can use RegistryKey.OpenBaseKey method. This method takes two arguments- RegistryHive and RegistryView. Here, seperate registry views are present for 32-bit and 64-bit.

Here is the sample C# code to read AppPaths for 32-bit and 64-bit applications installed on the system:

C#
try
                {
                    string AppPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
                    RegistryKey rkbase = null;
                    rkbase = RegistryKey.OpenBaseKey
                             (Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
                    using (RegistryKey rk = rkbase.OpenSubKey(uninstallKey))
                    {
                        foreach (string skName in rk.GetSubKeyNames())
                        {
                            using (RegistryKey sk = rk.OpenSubKey(skName))
                            {
                                try
                                {

                                    if (sk.GetValue("Path") != null)
                                    {
                                        //add this to required list
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                }
                            }
                        }
                    }

                    if (Environment.Is64BitOperatingSystem)
                    {
                        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(AppPath))
                        {
                            foreach (string skName in rk.GetSubKeyNames())
                            {
                                using (RegistryKey sk = rk.OpenSubKey(skName))
                                {
                                    try
                                    {
                                        if (sk.GetValue("Path") != null)
                                        {
                                            //add this to required list
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

Here, Environment.Is64BitOperatingSystem is used to check if the current system is 64-bit or not. This function is avialable with .NET Framework 4.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software Engineer having hands-on experience with C, C++, C#, .NET, ASP.NET, SQL, Website designing technologies, Joomla CMS, Application development, COM, MFC, Installshield, Installscript project, Basic MSI

http://newapputil.blogspot.in/
http://nvivekgoyal.blogspot.in/

Comments and Discussions

 
-- There are no messages in this forum --