最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

Accessing64bitregistryfroma32bitprocess

来源:懂视网 责编:小采 时间:2020-11-09 07:24:51
文档

Accessing64bitregistryfroma32bitprocess

Accessing64bitregistryfroma32bitprocess: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
推荐度:
导读Accessing64bitregistryfroma32bitprocess: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 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

}

///

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

///

/// Parent key to the key to be opened.

/// Name of the key to be opened

/// Whether or not this key is writable

/// 32-bit node or 64-bit node

///

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;

}

///

/// Get a pointer to a registry key.

///

/// Registry key to obtain the pointer of.

/// Pointer to the given registry key.

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;

}

///

/// Get a registry key from a pointer.

///

/// Pointer to the registry key

/// Whether or not the key is writable.

/// Whether or not we own the handle.

/// Registry key pointed to by the given pointer.

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/

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

Accessing64bitregistryfroma32bitprocess

Accessing64bitregistryfroma32bitprocess: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
推荐度:
标签: 32位 64位 pr
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top