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

来源:互联网 发布:2017盒子免费视频软件 编辑:程序博客网 时间:2024/04/30 05:31

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();
           
            }

        }