데이터 베이스 MySQL 튜토리얼 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/

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

PR의 정식 이름은 무엇입니까? PR의 정식 이름은 무엇입니까? Aug 22, 2022 pm 03:53 PM

PR의 정식 이름은 무엇입니까?

pr에 오디오 트랙이 있지만 소리가 나지 않을 때 문제를 해결하는 방법 pr에 오디오 트랙이 있지만 소리가 나지 않을 때 문제를 해결하는 방법 Jun 26, 2023 am 11:07 AM

pr에 오디오 트랙이 있지만 소리가 나지 않을 때 문제를 해결하는 방법

1비트는 몇 바이트와 같습니다. 1비트는 몇 바이트와 같습니다. Mar 09, 2023 pm 03:11 PM

1비트는 몇 바이트와 같습니다.

pr 파일의 압축 유형이 지원되지 않으면 어떻게 해야 합니까? pr 파일의 압축 유형이 지원되지 않으면 어떻게 해야 합니까? Mar 23, 2023 pm 03:12 PM

pr 파일의 압축 유형이 지원되지 않으면 어떻게 해야 합니까?

PR 자막은 단어 그대로 어떻게 표시되나요? PR 자막은 단어 그대로 어떻게 표시되나요? Aug 11, 2023 am 10:04 AM

PR 자막은 단어 그대로 어떻게 표시되나요?

PR에서 영상을 편집할 때 오류가 나면 어떻게 해야 하나요? PR에서 영상을 편집할 때 오류가 나면 어떻게 해야 하나요? Mar 22, 2023 pm 01:59 PM

PR에서 영상을 편집할 때 오류가 나면 어떻게 해야 하나요?

PR은 무슨 뜻인가요? PR은 무슨 뜻인가요? Aug 03, 2023 am 10:15 AM

PR은 무슨 뜻인가요?

PR자료를 타임라인으로 드래그할 수 없으면 어떻게 해야 하나요? PR자료를 타임라인으로 드래그할 수 없으면 어떻게 해야 하나요? Aug 10, 2023 pm 03:41 PM

PR자료를 타임라인으로 드래그할 수 없으면 어떻게 해야 하나요?

See all articles