控制Wow64重定向

参考:https://blog.csdn.net/karlxzy/article/details/42170419

 

在默认情况下,64位环境运行32位程序,会启用重定向,

比如 调用CreateFile时,系统会把system32文件夹重定向到Syswow64等等。


但是有些时候需要访问system32文件夹的时候就需要关闭重定向。

MS已经提供了一组函数用来控制重定向:

Wow64EnableWow64FsRedirection
Wow64DisableWow64FsRedirection
Wow64RevertWow64FsRedirection


用法在MSDN里面有DEMO:

 

  1.  
    #define _WIN32_WINNT 0x0501
  2.  
    #include <Windows.h>
  3.  
     
  4.  
    void main()
  5.  
    {
  6.  
    HANDLE hFile = INVALID_HANDLE_VALUE;
  7.  
    PVOID OldValue = NULL;
  8.  
     
  9.  
    // Disable redirection immediately prior to the native API
  10.  
    // function call.
  11.  
    if( Wow64DisableWow64FsRedirection(&OldValue) )
  12.  
    {
  13.  
    // Any function calls in this block of code should be as concise
  14.  
    // and as simple as possible to avoid unintended results.
  15.  
    hFile = CreateFile(TEXT("C:\\Windows\\System32\\Notepad.exe"),
  16.  
    GENERIC_READ,
  17.  
    FILE_SHARE_READ,
  18.  
    NULL,
  19.  
    OPEN_EXISTING,
  20.  
    FILE_ATTRIBUTE_NORMAL,
  21.  
    NULL);
  22.  
     
  23.  
    // Immediately re-enable redirection. Note that any resources
  24.  
    // associated with OldValue are cleaned up by this call.
  25.  
    if ( FALSE == Wow64RevertWow64FsRedirection(OldValue) )
  26.  
    {
  27.  
    // Failure to re-enable redirection should be considered
  28.  
    // a criticial failure and execution aborted.
  29.  
    return;
  30.  
    }
  31.  
    }
  32.  
     
  33.  
    // The handle, if valid, now can be used as usual, and without
  34.  
    // leaving redirection disabled.
  35.  
    if( INVALID_HANDLE_VALUE != hFile )
  36.  
    {
  37.  
    // Use the file handle
  38.  
    }
  39.  
    }


 

 

 

posted @ 2021-04-25 22:54  狂客  阅读(600)  评论(0编辑  收藏  举报