Disabling WOW64 File System Redirection from C#
[Posted on my MSN Space on June 28, 2005]
A long time ago on a blog now gone (looks like erablog.net disappeared!) I wrote a post about interacting with WOW64 (Windows on Windows 64) from a 32-bit application. The example I posted was unmanaged C++. Yesterday, a coworker wanted to implement the same functionality in C#. I don't know of any managed interfaces to WOW64 (if you know of one let me know) so I used Interop and DllImport to call Wow64EnableWow64FsRedirection from Kernel32.dll. A nice feature of interop is that you can simply throw the function in a try-catch block so that if the application is run on an OS that doesn't support the API (32-bit Windows XP, for example), you can simply catch the exception and continue. In the unmanaged C++ world you have to use runtime dynamic linking.
Here's what it looks like from unmanaged C++:
typedef WINBASEAPI BOOLEAN WINAPI LPFN_ENABLEFSREDIRECTION(BOOLEAN);
...
LPFN_ENABLEFSREDIRECTION *fnEnableFSRedirection;
fnEnableFSRedirection = (LPFN_ENABLEFSREDIRECTION*) GetProcAddress(GetModuleHandle("kernel32"),"Wow64EnableWow64FsRedirection");
if(NULL != fnEnableFSRedirection)
{
if(fnEnableFSRedirection(bEnable))
{
cprintf(TEXT("Success disabling FS Redirection"));
}
}
else
{
cprintf(TEXT("Error disabling FS Redirection"));
}
Here's what it looks like in C#:
class Wow64Mgr
{
[STAThread]
static void Main(string[] args)
{
try
{
if(Wow64Interop.EnableWow64FSRedirection(false))
Console.WriteLine("WOW64 File System Redirection Disabled");
else
Console.WriteLine("Failure Disabling WOW64 File System Redirection");
}
catch (Exception exc)
{
Console.WriteLine("Failure Setting WOW64 File System Redirection.");
Console.WriteLine(exc.Message);
}
}
}
public class Wow64Interop
{
[DllImport("Kernel32.Dll", EntryPoint="Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);
}
A long time ago on a blog now gone (looks like erablog.net disappeared!) I wrote a post about interacting with WOW64 (Windows on Windows 64) from a 32-bit application. The example I posted was unmanaged C++. Yesterday, a coworker wanted to implement the same functionality in C#. I don't know of any managed interfaces to WOW64 (if you know of one let me know) so I used Interop and DllImport to call Wow64EnableWow64FsRedirection from Kernel32.dll. A nice feature of interop is that you can simply throw the function in a try-catch block so that if the application is run on an OS that doesn't support the API (32-bit Windows XP, for example), you can simply catch the exception and continue. In the unmanaged C++ world you have to use runtime dynamic linking.
Here's what it looks like from unmanaged C++:
typedef WINBASEAPI BOOLEAN WINAPI LPFN_ENABLEFSREDIRECTION(BOOLEAN);
...
LPFN_ENABLEFSREDIRECTION *fnEnableFSRedirection;
fnEnableFSRedirection = (LPFN_ENABLEFSREDIRECTION*) GetProcAddress(GetModuleHandle("kernel32"),"Wow64EnableWow64FsRedirection");
if(NULL != fnEnableFSRedirection)
{
if(fnEnableFSRedirection(bEnable))
{
cprintf(TEXT("Success disabling FS Redirection"));
}
}
else
{
cprintf(TEXT("Error disabling FS Redirection"));
}
Here's what it looks like in C#:
class Wow64Mgr
{
[STAThread]
static void Main(string[] args)
{
try
{
if(Wow64Interop.EnableWow64FSRedirection(false))
Console.WriteLine("WOW64 File System Redirection Disabled");
else
Console.WriteLine("Failure Disabling WOW64 File System Redirection");
}
catch (Exception exc)
{
Console.WriteLine("Failure Setting WOW64 File System Redirection.");
Console.WriteLine(exc.Message);
}
}
}
public class Wow64Interop
{
[DllImport("Kernel32.Dll", EntryPoint="Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);
}