32位程序如何访问64位的注册表

32位程序如何访问64位的注册表
64位OS中,从32位Nunit调用32位DLL中的方法运行,如何访问64位的注册表项? 
(也即关闭注册表转向功能。) 

如读取HKEY_LOCAL_MACHINE/Software/Microsoft子项的value,而非   HKEY_LOCAL_MACHINE/Software/Wow6432Node/Microsoft子项的value。 

我在网上,找到一些这方面的资料:   http://www.cnblogs.com/FlyingBread/archive/2007/01/21/624291.aspx 
http://msdn2.microsoft.com/en-us/library/aa365744.aspx   

然后试着编码,如下,运行,结果是失败,无法打开HKEY_LOCAL_MACHINE/Software/Microsoft/Updates: 

                [Test] 
                public   void   test() 
                { 
                        bool   update   =   false; 

                        IntPtr   ptr   =   new   IntPtr(1); 

                        Wow64DisableWow64FsRedirection(ref   ptr); 

                        RegistryKey   softKey   =   Registry.LocalMachine; 
                        RegistryKey   updateKey   =   softKey.OpenSubKey(@ "Software/Microsoft/Updates "); 

                        //   if   the   key   is   null 
                        if   (updateKey   !=   null) 
                        { 
                                update   =   true; 
                        } 

                        Wow64RevertWow64FsRedirection(ptr); 

                        Assert.IsTrue(update);     
                } 

可有高手来指教一下?本人因为分数太少,就只能给20分了,见谅!!!

 

 

 

 

 

用RegOpenKeyEx试试看。

 

 

许查了资料,解决了这个问题。核心代码如下

public string Get() { 
            try 
            { 
                //the registry handle we get from regopenkeyex. 
                UIntPtr pHKey = new UIntPtr(); 
                //result string 
                StringBuilder result = new StringBuilder(); 
                //NOTE!!!!!!!!!!!!! This code should be improved 
                uint resultSize = 1024; 
                uint lpType = 0; 

                //Disble redirection 
                IntPtr oldWOW64State = new IntPtr(); 
                if (Win32Encap.Win32Encap.Wow64DisableWow64FsRedirection(ref oldWOW64State)) 
                { 

                    //get the key handle 
                    CrossWOW64RegVisitor.Win32Encap.Win32Encap.RegOpenKeyEx((UIntPtr)this.BaseRoot, 
                        this.KeyPath, 
                        0, (int)this.SAM | (int)(SAM.KEY_QUERY_VALUE), 
                        ref pHKey); 

                    //Disable reflection 
                    Win32Encap.Win32Encap.RegDisableReflectionKey(pHKey); 

                    //Get value 
                    CrossWOW64RegVisitor.Win32Encap.Win32Encap.RegQueryValueEx(pHKey, this.ValueName, 0, out lpType, result, ref resultSize); 

                    //Enable reflection 
                    Win32Encap.Win32Encap.RegEnableReflectionKey(pHKey); 

                } 


                Win32Encap.Win32Encap.Wow64RevertWow64FsRedirection(oldWOW64State); 


                return result.ToString(); 
            } 
                //if caught, there must some API not available, just return the normal value 
            catch (Exception ex) { 

                string fullKeyPath = this.BaseRoot.ToString() + "//" + this.KeyPath; 
                return Registry.GetValue(fullKeyPath, this.ValueName, "").ToString(); 
            
            } 

        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值