Heim Datenbank 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 &gt;.

    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/

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße Artikel -Tags

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Wie lautet der vollständige Name von PR? Wie lautet der vollständige Name von PR? Aug 22, 2022 pm 03:53 PM

Wie lautet der vollständige Name von PR?

So lösen Sie das Problem, wenn PR eine Audiospur, aber keinen Ton hat So lösen Sie das Problem, wenn PR eine Audiospur, aber keinen Ton hat Jun 26, 2023 am 11:07 AM

So lösen Sie das Problem, wenn PR eine Audiospur, aber keinen Ton hat

Was soll ich tun, wenn der Komprimierungstyp der PR-Datei nicht unterstützt wird? Was soll ich tun, wenn der Komprimierungstyp der PR-Datei nicht unterstützt wird? Mar 23, 2023 pm 03:12 PM

Was soll ich tun, wenn der Komprimierungstyp der PR-Datei nicht unterstützt wird?

1 Bit entspricht der Anzahl der Bytes 1 Bit entspricht der Anzahl der Bytes Mar 09, 2023 pm 03:11 PM

1 Bit entspricht der Anzahl der Bytes

Wie erscheinen PR-Untertitel Wort für Wort? Wie erscheinen PR-Untertitel Wort für Wort? Aug 11, 2023 am 10:04 AM

Wie erscheinen PR-Untertitel Wort für Wort?

Was tun, wenn beim Zusammenstellen eines Videos in PR ein Fehler auftritt? Was tun, wenn beim Zusammenstellen eines Videos in PR ein Fehler auftritt? Mar 22, 2023 pm 01:59 PM

Was tun, wenn beim Zusammenstellen eines Videos in PR ein Fehler auftritt?

Was bedeutet PR? Was bedeutet PR? Aug 03, 2023 am 10:15 AM

Was bedeutet PR?

Was soll ich tun, wenn das PR-Material nicht auf die Timeline gezogen werden kann? Was soll ich tun, wenn das PR-Material nicht auf die Timeline gezogen werden kann? Aug 10, 2023 pm 03:41 PM

Was soll ich tun, wenn das PR-Material nicht auf die Timeline gezogen werden kann?

See all articles