Click here to Skip to main content
14,980,986 members
Articles / Programming Languages / C#
Tip/Trick
Posted 9 Aug 2017

Stats

17.6K views
155 downloads
16 bookmarked

Registry Redirection when using 32-bit Application on 64-bit Windows

Rate me:
Please Sign up or sign in to vote.
4.10/5 (19 votes)
4 Sep 2017CPOL1 min read
The tip explains Windows registry redirection feature with a simple example

Introduction

This tip will guide you through Windows registry redirection feature, which might seem quite unintuitive at first acquaintance.

Using the Code

Consider the following situation.
We have code that writes to HKEY_LOCAL_MACHINE registry on 64-bit OS.

C#
var softwareSubKey = Registry.LocalMachine.OpenSubKey
                     ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
softwareSubKey.CreateSubKey("MySoftware");

When successfully executing this code, we somewhat unexpectedly receive no MySoftware folder at \HKEY_LOCAL_MACHINE\SOFTWARE path.
After investigating build tab in project options, we discover the following:

Image 1

After changing Platform target to x64, we receive an expected result.

Image 2

Why Does This Happen?

The reason is registry redirector which separates 32 and 64-bit applications providing separate views for them. So in fact when we chose x86 platform target, we just get our key redirected to \HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node. Same handles refer to different physical registry location for different target platforms.

Image 3

As you might expect, registry redirector consistently works with the same physical location. Thus, if we expand our example to deleting created registry folder:

C#
private static void Write()
{
    var softwareSubKey = Registry.LocalMachine.OpenSubKey
                         ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
    softwareSubKey.CreateSubKey("MySoftware");           
}

private static void Delete()
{
    var softwareSubKey = Registry.LocalMachine.OpenSubKey
                         ("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
    try
    {
        softwareSubKey.DeleteSubKeyTree("MySoftware", true);
    }
    catch (ArgumentException)
    {
        Console.WriteLine("Gotcha!");
        Console.ReadKey();
    }           
}

static void Main(string[] args)
{
    Write();
    Delete();
}

ArgumentException won't be fired as we work with the same physical location.

Although if we use 32-bit application and will create manually or by 64-bit application entry in the 64-bit physical location, we'll fail with argument exception as expected.

Image 4

Avoiding Registry Redirection

You can forcibly write to 32-bit view with 64-bit application and vice versa using RegistryKey.OpenBaseKey overload which accepts RegistryView as parameter. For example, the code below writes to \HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node regardless of platform target.

C#
var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var softwareSubKey = localMachine.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
softwareSubKey.CreateSubKey("MySoftware");

On Which Keys Does This Apply?

To know on which keys this applies, use documentation.

Points of Interest

Writing and reading into several registry folders requires elevated permission.
To make the application run under elevated permissions, specify in app.manifest.

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

Also to enable write, use the following overload of OpenSubKey:

C#
Registry.LocalMachine.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);

History

  • 22.08.2017 - Added "Avoiding Registry Redirection" section
  • 04.09.2017 - Fixed small typo

License

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

Share

About the Author

Bohdan Stupak
Software Developer
Ukraine Ukraine
https://twitter.com/BohdanStupak1

Comments and Discussions

 
QuestionMinor Typo Pin
Joe Pizzi1-Sep-17 14:07
MemberJoe Pizzi1-Sep-17 14:07 
AnswerRe: Minor Typo Pin
Bohdan Stupak4-Sep-17 0:20
professionalBohdan Stupak4-Sep-17 0:20 
QuestionSuggestion... Pin
i0013-Aug-17 13:22
Memberi0013-Aug-17 13:22 
AnswerRe: Suggestion... Pin
Bohdan Stupak21-Aug-17 21:11
professionalBohdan Stupak21-Aug-17 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.