1. 程式人生 > >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();
           
            }

        }