2e2e2 发表于 2014-4-18 11:47:02

win7重定向函数引发的问题

在Win7 64位系统下,调用CopyFile函数进行函数拷贝,如果希望拷贝文件到System32目录下,需要考虑程序是不是也是64位的,否则,程序会被重定向到Syswow64目录下,为了防止出现文件被重定向,就需要调用函数Wow64DisableWow64FsRedirection取消文件的重定向,并在完成指定功能后调用Wow64RevertWow64FsRedirection函数进行恢复。
MSDN中关于函数Wow64DisableWow64FsRedirection的声明:
BOOL WINAPI Wow64DisableWow64FsRedirection(
__outPVOID *OldValue
);
http://msdn.microsoft.com/en-us/library/aa365743(v=vs.85).aspx
MSDN中关于函数Wow64RevertWow64FsRedirection的声明:
BOOL WINAPI Wow64RevertWow64FsRedirection(
__inPVOID OldValue
);
http://msdn.microsoft.com/en-us/library/aa365745(v=vs.85).aspx
关于函数的使用,摘一段msdn中的代码:
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0501
#ifdef NTDDI_VERSION
#undef NTDDI_VERSION
#endif
#define NTDDI_VERSION 0x05010000
#include <Windows.h>
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
    }
}
这段代码在win764位下调用没有问题,在xp 32位下面就会出现问题,即使加上判断系统是否为64位的代码也会导致程序出现如下图的错误:
解决方案是使用动态加载的方式对Wow64DisableWow64FsRedirection和Wow64RevertWow64FsRedirection进行加载,这样问题就解决了
PVOID OldValue = NULL;
HINSTANCEhlibrary;
typedef    int      (__stdcall*Wow64DisableWow64FsRedirection)(LPVOID);
Wow64DisableWow64FsRedirectionf_Wow64DisableWow64FsRedirection=NULL;
typedef    int      (__stdcall*Wow64RevertWow64FsRedirection)(LPVOID);
Wow64RevertWow64FsRedirectionf_Wow64RevertWow64FsRedirection=NULL;
hlibrary=LoadLibrary(L"Kernel32.dll");
   f_Wow64DisableWow64FsRedirection=(Wow64DisableWow64FsRedirection)GetProcAddress(hlibrary,"Wow64DisableWow64FsRedirection");
   if(!f_Wow64DisableWow64FsRedirection) {
    return 0;
   }
   f_Wow64RevertWow64FsRedirection=(Wow64RevertWow64FsRedirection)GetProcAddress(hlibrary,"Wow64RevertWow64FsRedirection");
   if(!f_Wow64RevertWow64FsRedirection) {
    return 0;
   }
   f_Wow64DisableWow64FsRedirection(&OldValue);
   if(CopyFile(wNativePath,wSystemPath,FALSE)==0)
   {
    if ( FALSE == Wow64RevertWow64FsRedirection(OldValue) )
    {
   return 0;
    }
    return 0;
   }
   f_Wow64RevertWow64FsRedirection(OldValue);
   FreeLibrary(hlibrary);
在使用这段代码前最好判断一下系统类型及位数!

页: [1]
查看完整版本: win7重定向函数引发的问题