using System;
using static Vanara.PInvoke.Kernel32;
namespace Vanara.IO
{
///
/// Suspends File System Redirection if found to be in effect. Effectively, this calls IsWow64Process to determine state and then disables
/// redirection using Wow64DisableWow64FsRedirection. It then reverts redirection at disposal using Wow64RevertWow64FsRedirection.
///
///
/// This class is best used in a using clause as follows:
///
/// using (new Wow64Redirect())
/// {
/// if (System.IO.File.Exists(@"C:\Windows\System32\qmgr.dll"))
/// {
/// // Do something
/// }
/// }
///
///
public class Wow64Redirect : IDisposable
{
private readonly bool isWow64;
private readonly IntPtr oldVal;
/// Initializes a new instance of the class.
public Wow64Redirect()
{
if (isWow64 = (IsWow64Process(GetCurrentProcess(), out var wow) && wow))
Wow64DisableWow64FsRedirection(out oldVal);
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
void IDisposable.Dispose()
{
if (isWow64)
Wow64RevertWow64FsRedirection(oldVal);
}
}
}