2012年7月2日 星期一

Wow64DisableWow64FsRedirection


To access 64-bit registry key from 32-bit application, you may specify KEY_WOW64_64KEY flag to RegOpenKeyEx method. this article shows a demo about how to access 64-bit registry from a 32-bit application in managed code, hope it can help.

We can use API Wow64DisableWow64FsRedirection to disable File System Redirection, and revert it with API Wow64RevertWow64FsRedirection, here is some code snippets:

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
       
        static void Main(string[] args)
        {
            Console.WriteLine("FsRedirection enabled.");

            // Physical path is "C:\Windows\SysWOW64\test\32"
            Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\32"));
            // Physical path is "C:\Windows\System32\test\64\"
            Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\64"));

            IntPtr ptr = IntPtr.Zero;
            if (Wow64DisableWow64FsRedirection(ref ptr))
            {
                Console.WriteLine("FsRedirection disabled.");

                Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\32"));
                Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\64"));

                if (Wow64RevertWow64FsRedirection(ptr))
                {
                    Console.WriteLine("FsRedirection enabled.");

                    Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\32"));
                    Console.WriteLine(Directory.Exists(@"C:\Windows\System32\test\64"));
                }
            }

            Console.ReadLine();
        }

Since the Wow64DisableWow64FsRedirection function affects all file operations performed by the current thread, which can have unintended consequences if file system redirection is disabled for any length of time (For example, DLL loading depends on file system redirection), disabling file system redirection will cause DLL loading to fail.  To avoid these problems, disable file system redirection immediately before calls to specific file I/O functions (such as CreateFile) that must not be redirected, and re-enable file system redirection immediately afterward using Wow64RevertWow64FsRedirection.

沒有留言:

張貼留言