64位读取注册表与32位的区别

简介: 有一个读取注册表信息的程序  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0, KEY_READ, &hKey) == ERROR_SUCCESS)/,在32位下完全正常,但是在64位返回值正确,但就是读不到东西。

有一个读取注册表信息的程序  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0, KEY_READ, &hKey) == ERROR_SUCCESS)/

,在32位下完全正常,但是在64位返回值正确,但就是读不到东西。后来单步发现读不到东西,就搜64位读注册表失败,发现需要加

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkeystring , 0,KEY_READ|KEY_WOW64_64KEY, &hKey) == ERROR_SUCCESS)就可以了,我是全部把权限提高,还可以根据不同的操作系统,设置不同的参数。

 

IsWow64Process 判断64位操作系统

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
IsWow64返回TRUE则是64位系统,否则为32位系统。
BOOL IsWow64()
{
    BOOL bIsWow64 = FALSE;
    fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
        GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
        {
                        return FALSE;
        }
    }
    return bIsWow64;
}

可参考的文献:

 

http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx

http://www.codeproject.com/Articles/51326/Net-Compilation-registry-accessing-and-applicatio

http://boluns.blog.163.com/blog/static/69845968201071132032313/

 

 

 

The code

The next code is a personal translation to C# of several pieces of code I found, mostly from here:

[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 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; }

How to use the Code

The OpenSubKey will return the searched key, allowing you to specify reading from the normal registry, or from the alternative 32-bit, WOW64 registry. The following example reads from the 32-bit WOW64 registry:

try 
{ 
    RegistryKey key = OpenSubKey(Registry.LocalMachine,"Software\\[Key]",false,
        eRegWow64Options.KEY_WOW64_32KEY); 
}
catch 
{ 
    // Parent key not open, exception found at opening (probably related to // security permissions requested) }

You just need to place your key name where “[Key]” is.

目录
相关文章
|
2月前
|
人工智能 缓存 JSON
AI 智能体的开发及上线
本项目开发一款专注股票财报可视化分析的AI智能体,融合规划、记忆、工具调用与大模型推理能力,可自主诊断异常、调取数据、生成图表及白话解读。采用Dify等低代码框架快速落地,集成RAG记忆、结构化输出与缓存优化,兼顾专业性、合规性与低成本。(239字)
|
5月前
|
存储 人工智能 安全
阿里云/本地部署OpenClaw多Agent协作指南:11个AI助理协同搭建+大模型api配置教程
2026年,OpenClaw的多Agent架构彻底颠覆了单一AI助理的使用模式——通过拆分角色、隔离资源、精准路由,让多个AI助理各司其职、协同作战,完美解决“全能但不专精”的痛点。无论是内容创作、代码开发,还是数据分析、日常协作,都能通过专属Agent实现“专人专事”的高效闭环。
1583 2
|
6月前
|
数据采集 人工智能 数据可视化
从0开始全面认识高质量数据集建设(2)
本文系统阐述高质量数据集建设的“共建共治”管理模式:以统一标准为前提,推动源头生产、集中监管;以一套标准、一个目录、一套工具为支柱,实现需求精准转化、多源数据质量管控与全生命周期数字化治理,支撑AI场景高效落地。
|
缓存 监控 网络协议
掌控全局:Linux 系统性能调优技巧全面指南
掌控全局:Linux 系统性能调优技巧全面指南
|
UED
一些 CSS3 动画的实际应用案例
一些 CSS3 动画的实际应用案例
642 57
|
存储 安全 Java
Java 基础篇必背综合知识点全面总结
本文总结了Java基础篇的核心知识点,涵盖Java特性、JDK与JRE、数据类型与运算符、流程控制语句、面向对象编程(类与对象、封装、继承、多态)、常用类库(java.lang、java.util、java.io)等内容。同时,还介绍了字符串处理、Servlet隐式对象及请求转发与重定向等重要概念。通过学习这些基础知识,可为深入掌握Java高级特性和实际开发打下坚实基础。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
545 0
|
存储 物联网 数据处理
什么数据中心最好?盘点全球十大数据中心!
在数字时代,数据中心作为关键基础设施,支撑着商业和社会的高效运转。从AWS、谷歌、微软到阿里云、苹果等巨头的数据中心,它们各具特色,涵盖高性能计算、液冷技术、绿色节能和高安全性等领域。这些“超级堡垒”不仅保障了在线交易、远程教育、智慧医疗等服务的稳定运行,还推动了云计算、大数据和物联网的发展,极大提升了社会效率和生活质量。每个数据中心根据自身优势,在不同应用场景中发挥着不可替代的作用,共同构建了数字化世界的基石。
1700 1
|
JavaScript 前端开发 开发者
摸鱼必备!!10个你不知道的 Vue 3 组件库...
摸鱼必备!!10个你不知道的 Vue 3 组件库...
|
机器学习/深度学习 算法 数据安全/隐私保护
基于FPGA的SNN脉冲神经网络之LIF神经元verilog实现,包含testbench
本项目展示了 LIF(Leaky Integrate-and-Fire)神经元算法的实现与应用,含无水印运行效果预览。基于 Vivado2019.2 开发,完整代码配有中文注释及操作视频。LIF 模型模拟生物神经元特性,通过积分输入信号并判断膜电位是否达阈值产生脉冲,相较于 Hodgkin-Huxley 模型更简化,适合大规模神经网络模拟。核心程序片段示例,助您快速上手。
|
存储 安全 Java
2024ide构建maven项目是总是卡在解析Maven依赖项目 加速方案
2024ide构建maven项目是总是卡在解析Maven依赖项目 加速方案
912 4
2024ide构建maven项目是总是卡在解析Maven依赖项目 加速方案

热门文章

最新文章