Sunday, January 30, 2011

Disables file system redirection in 64bit OS

Tip - Wow64DisableWow64FsRedirection() Wow64RevertWow64FsRedirection()

Details - This function is useful for 32-bit applications that want to gain access to the native “%SystemRoot%\System32” directory in 64bit OS. By default, WOW64 file system redirection is enabled.

BOOL WINAPI Wow64DisableWow64FsRedirection(
  __out  PVOID *OldValue
);

To restore file system redirection, the Wow64RevertWow64FsRedirection function is used. Every successful call to the Wow64DisableWow64FsRedirection function must have a matching call to the Wow64RevertWow64FsRedirection function. This will ensure redirection is re-enabled and frees associated system resources.

BOOL WINAPI Wow64RevertWow64FsRedirection(
  __in  PVOID OldValue
);

The following example uses Wow64DisableWow64FsRedirection to disable file system redirection so that a 32-bit application that is running under WOW64 can open the 64-bit version of Notepad.exe in %SystemRoot%\System32 instead of being redirected to the 32-bit version in %SystemRoot%\SysWOW64.

void main()
{
    HANDLE hFile = INVALID_HANDLE_VALUE;
    PVOID OldValue = NULL;

    //  Disable redirection immediately prior to the native API
    //  function call.
    if( Wow64DisableWow64FsRedirection(&OldValue) )
    {
        //  Any function calls in this block of code should be as concise
        //  and as simple as possible to avoid unintended results.
        hFile = CreateFile(TEXT("C:\\Windows\\System32\\Notepad.exe"),
            GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);

        //  Immediately re-enable redirection. Note that any resources
        //  associated with OldValue are cleaned up by this call.
        if ( FALSE == Wow64RevertWow64FsRedirection(OldValue) )
        {
            //  Failure to re-enable redirection should be considered
            //  a criticial failure and execution aborted.
            return;
        }
    }
   
    //  The handle, if valid, now can be used as usual, and without
    //  leaving redirection disabled.
    if( INVALID_HANDLE_VALUE != hFile ) 
    {
        // Use the file handle
    }
}

Reference   -

Posted By : Velayudhan Pillai K

No comments:

Post a Comment