Home Database Mysql Tutorial Accessing 64 bit registry from a 32 bit process

Accessing 64 bit registry from a 32 bit process

Jun 07, 2016 pm 03:49 PM
bit from pr registry

As you may know, Windows is virtualizing some parts of the registry under 64 bit. So if you try to open, for example, this key : “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90″, from a 32 bit C# application running on a 6

As you may know, Windows is virtualizing some parts of the registry under 64 bit.

So if you try to open, for example, this key : “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90″, from a 32 bit C# application running on a 64 bit system, you will be redirected to : “HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\90″

Why ? Because Windows uses the Wow6432Node registry entry to present a separate view of HKEY_LOCAL_MACHINE\SOFTWARE for 32 bit applications that runs on a 64 bit systems.

If you want to explicitly open the 64 bit view of the registry, here is what you have to perform :

You are using VS 2010 and version 4.x of the .NET framework

It’s really simple, all you need to do is, instead of doing :

//Will redirect you to the 32 bit view

RegistryKey sqlsrvKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\90");

do the following :

RegistryKey localMachineX64View = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

RegistryKey sqlsrvKey = localMachineX64View.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\90");


Prior versions of the .NET framework

For the prior versions of the framework, we have to use P/Invoke and call the function RegOpenKeyExW with parameter KEY_WOW64_64KEY

enum RegWow64Options

{

    None = 0,

    KEY_WOW64_64KEY = 0x0100,

    KEY_WOW64_32KEY = 0x0200

}

 

enum RegistryRights

{

    ReadKey = 131097,

    WriteKey = 131078

}

 

/// <summary></summary>

/// Open a registry key using the Wow64 node instead of the default 32-bit node.

///

/// <param name="parentKey">Parent key to the key to be opened.

/// <param name="subKeyName">Name of the key to be opened

/// <param name="writable">Whether or not this key is writable

/// <param name="options">32-bit node or 64-bit node

/// <returns></returns>

static RegistryKey _openSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options)

{

    //Sanity check

    if (parentKey == null || _getRegistryKeyHandle(parentKey) == IntPtr.Zero)

    {

        return null;

    }

 

    //Set rights

    int rights = (int)RegistryRights.ReadKey;

    if (writable)

        rights = (int)RegistryRights.WriteKey;

 

    //Call the native function >.

    int subKeyHandle, result = RegOpenKeyEx(_getRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)options, out subKeyHandle);

 

    //If we errored, return null

    if (result != 0)

    {

        return null;

    }

 

    //Get the key represented by the pointer returned by RegOpenKeyEx

    RegistryKey subKey = _pointerToRegistryKey((IntPtr)subKeyHandle, writable, false);

    return subKey;

}

 

/// <summary></summary>

/// Get a pointer to a registry key.

///

/// <param name="registryKey">Registry key to obtain the pointer of.

/// <returns>Pointer to the given registry key.</returns>

static IntPtr _getRegistryKeyHandle(RegistryKey registryKey)

{

    //Get the type of the RegistryKey

    Type registryKeyType = typeof(RegistryKey);

    //Get the FieldInfo of the 'hkey' member of RegistryKey

    System.Reflection.FieldInfo fieldInfo =

    registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

 

    //Get the handle held by hkey

    SafeHandle handle = (SafeHandle)fieldInfo.GetValue(registryKey);

    //Get the unsafe handle

    IntPtr dangerousHandle = handle.DangerousGetHandle();

    return dangerousHandle;

}

 

/// <summary></summary>

/// Get a registry key from a pointer.

///

/// <param name="hKey">Pointer to the registry key

/// <param name="writable">Whether or not the key is writable.

/// <param name="ownsHandle">Whether or not we own the handle.

/// <returns>Registry key pointed to by the given pointer.</returns>

static RegistryKey _pointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle)

{

    //Get the BindingFlags for private contructors

    System.Reflection.BindingFlags privateConstructors = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;

    //Get the Type for the SafeRegistryHandle

    Type safeRegistryHandleType = typeof(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");

    //Get the array of types matching the args of the ctor we want

    Type[] safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) };

    //Get the constructorinfo for our object

    System.Reflection.ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(

    privateConstructors, null, safeRegistryHandleCtorTypes, null);

    //Invoke the constructor, getting us a SafeRegistryHandle

    Object safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle });

 

    //Get the type of a RegistryKey

    Type registryKeyType = typeof(RegistryKey);

    //Get the array of types matching the args of the ctor we want

    Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(bool) };

    //Get the constructorinfo for our object

    System.Reflection.ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(

    privateConstructors, null, registryKeyConstructorTypes, null);

    //Invoke the constructor, getting us a RegistryKey

    RegistryKey resultKey = (RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable });

    //return the resulting key

    return resultKey;

}

 

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]

public static extern int RegOpenKeyEx(IntPtr hKey, string subKey, int ulOptions, int samDesired, out int phkResult);


Then we can open our registry key like this :

RegistryKey sqlsrvKey = _openSubKey(Registry.LocalMachine, @"SOFTWARE\Microsoft\Microsoft SQL Server\90", false, RegWow64Options.KEY_WOW64_64KEY);

As you can see, the framework 4 make our life easier.


Referenced from: http://dotnetgalactics.wordpress.com/2010/05/10/accessing-64-bit-registry-from-a-32-bit-process/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the full name of PR? What is the full name of PR? Aug 22, 2022 pm 03:53 PM

The full name of PR is "Adobe Premiere Pro"; PR is a video editing software developed by Adobe. It has good compatibility and can cooperate with other software launched by Adobe. It is widely used in advertising production and TV programs. making.

How to solve the problem when pr has audio track but no sound How to solve the problem when pr has audio track but no sound Jun 26, 2023 am 11:07 AM

PR has an audio track but no sound. Solution: 1. In the PR application, drag the material into the timeline; 2. In the edit menu, open the preferences; 3. In the preferences window, open the audio hardware item bar and find Default output option box; 4. In the option box, find the speaker option and click the OK button; 5. Return to the PR application, play it in the video preview window, and the sound will be broadcast.

What should I do if the compression type of the pr file is not supported? What should I do if the compression type of the pr file is not supported? Mar 23, 2023 pm 03:12 PM

Reasons and solutions for the unsupported compression type of PR files: 1. The streamlined version of PR has streamlined many video encoders. Reinstall and use the full version of Premiere; 2. Caused by irregular video encoding, you can use the format factory to Convert the video to WMV format.

1 bit equals how many bytes 1 bit equals how many bytes Mar 09, 2023 pm 03:11 PM

1 bit is equal to one-eighth of a byte. In the binary number system, each 0 or 1 is a bit (bit), and a bit is the smallest unit of data storage; every 8 bits (bit, abbreviated as b) constitute a byte (Byte), so "1 byte ( Byte) = 8 bits”. In most computer systems, a byte is an 8-bit (bit) long data unit. Most computers use a byte to represent a character, number, or other character.

How do PR subtitles appear word for word? How do PR subtitles appear word for word? Aug 11, 2023 am 10:04 AM

Methods for pr subtitles to appear verbatim: 1. Create a subtitle track; 2. Add subtitle text; 3. Adjust the duration; 4. Appear verbatim effect; 5. Adjust animation effects; 6. Adjust the position and transparency of subtitles; 7. Preview and export videos.

What to do if there is an error when compiling a video in PR What to do if there is an error when compiling a video in PR Mar 22, 2023 pm 01:59 PM

Solution to the error when compiling a video in PR: 1. Open the Premiere post-editing software on your computer, and then select "General" in the right menu bar of the project settings; 2. Enter the general settings window of Premiere and select "Mercury only" Playback Engine Software"; 3. Click "Confirm" to solve the error when compiling the video in PR.

What does PR mean? What does PR mean? Aug 03, 2023 am 10:15 AM

PR, short for Public Relations, is an important organizational management tool designed to improve an organization's reputation and trust by establishing and maintaining good relationships. It requires transparency, authenticity and consistency, while being tightly integrated with new and social media. Through effective PR practices, organizations can gain wider recognition and support, improving their competitiveness and sustainable development capabilities.

The operation process of closing the registry process in WIN10 The operation process of closing the registry process in WIN10 Mar 27, 2024 pm 05:51 PM

1. Press and hold the [Win+R] shortcut key combination on the keyboard, open the [Run] dialogue command window, enter the [services.msc] command, and click [OK];. 2. After opening the service interface, find the [RemoteRegistry] option, and double-click with the left button to open its properties dialog window. 3. In the [RemoteRegistry Properties] dialog window that opens, select the [Disabled] option in the startup type option, and then click the [Apply]--[Stop]--[OK] button to save the settings.

See all articles