注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

_

_

 
 
 

日志

 
 

x86程序读取64位系统注册表失败解决方案  

2012-12-13 20:25:37|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

?

在做一个项目时,遇到了这么一种情况,在64位操作系统下(WOW),运行一个x86的应用程序,这个程序会读取注册表项目,读取注册表的代码是一样的,只要不交叉访问就是正确的(32位程序运行在x86平台,64位程序运行在x64平台),最后进行了一番调试,才搞明白失败的原因。

在致原因是这样的,32位与64位两个操作系统版本上的注册表的结构是不一样的,有些键是可以共享的,而有些键是不可以共享的,这个我没有研究得很深入,记得在是MSDN上面看到过,不过链接给忘记了。

最后我在网上查一下,找到了一种解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32.SafeHandles;

namespace Common
{
??? public class RegUtil
??? {
??????? [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
??????? static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int sam, out IntPtr phkResult);

??????? [Flags]
??????? public enum eRegWow64Options : int
??????? {
??????????? None = 0x0000,
??????????? KEY_WOW64_64KEY = 0x0100,
??????????? KEY_WOW64_32KEY = 0x0200,
??????????? // Add here any others needed, from the table of the previous chapter
??????? }

??????? [Flags]
??????? public enum eRegistryRights : int
??????? {
??????????? ReadKey = 131097,
??????????? WriteKey = 131078,
??????? }

??????? public static RegistryKey OpenSubKey(RegistryKey pParentKey, string pSubKeyName, bool pWriteable, eRegWow64Options pOptions)
??????? {
??????????? if (pParentKey == null || GetRegistryKeyHandle(pParentKey).Equals(System.IntPtr.Zero))
??????????? {
??????????????? throw new System.Exception("OpenSubKey: Parent key is not open");
??????????? }

??????????? eRegistryRights Rights = eRegistryRights.ReadKey;
??????????? if (pWriteable)
??????????? {
??????????????? Rights = eRegistryRights.WriteKey;
??????????? }

??????????? System.IntPtr SubKeyHandle;
??????????? System.Int32 Result = RegOpenKeyEx(GetRegistryKeyHandle(pParentKey), pSubKeyName, 0,
????????????????????????????????????????????? (int)Rights | (int)pOptions, out SubKeyHandle);
??????????? if (Result != 0)
??????????? {
??????????????? System.ComponentModel.Win32Exception W32ex = new System.ComponentModel.Win32Exception();
??????????????? //throw new System.Exception("OpenSubKey: Exception encountered opening key", W32ex);
??????????????? return null;
??????????? }

??????????? return PointerToRegistryKey(SubKeyHandle, pWriteable, false);
??????? }

??????? private static System.IntPtr GetRegistryKeyHandle(RegistryKey pRegisteryKey)
??????? {
??????????? Type Type = Type.GetType("Microsoft.Win32.RegistryKey");
??????????? FieldInfo Info = Type.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);

??????????? SafeHandle Handle = (SafeHandle)Info.GetValue(pRegisteryKey);
??????????? IntPtr RealHandle = Handle.DangerousGetHandle();

??????????? return Handle.DangerousGetHandle();
??????? }

??????? private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool pWritable, bool pOwnsHandle)
??????? {
??????????? // Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
??????????? BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic;
??????????? Type safeRegistryHandleType = typeof(
??????????????? SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType(
??????????????? "Microsoft.Win32.SafeHandles.SafeRegistryHandle");

??????????? Type[] safeRegistryHandleConstructorTypes = new Type[] { typeof(System.IntPtr), typeof(System.Boolean) };
??????????? ConstructorInfo safeRegistryHandleConstructor =
??????????????? safeRegistryHandleType.GetConstructor(privateConstructors,
??????????????? null, safeRegistryHandleConstructorTypes, null);
??????????? Object safeHandle = safeRegistryHandleConstructor.Invoke(new Object[] { hKey, pOwnsHandle });

??????????? // Create a new Registry key using the private constructor using the
??????????? // safeHandle - this should then behave like
??????????? // a .NET natively opened handle and disposed of correctly
??????????? Type registryKeyType = typeof(Microsoft.Win32.RegistryKey);
??????????? Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(Boolean) };
??????????? ConstructorInfo registryKeyConstructor =
??????????????? registryKeyType.GetConstructor(privateConstructors, null,
??????????????? registryKeyConstructorTypes, null);
??????????? RegistryKey result = (RegistryKey)registryKeyConstructor.Invoke(new Object[] { safeHandle, pWritable });

??????????? return result;
??????? }
??? }
}

  评论这张
 
阅读(704)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017