C++ Wow64DisableWow64FsRedirection()函数在Visual Studio窗口10中为64位编译时不工作

C++ Wow64DisableWow64FsRedirection()函数在Visual Studio窗口10中为64位编译时不工作,c++,visual-studio-2010,visual-c++,C++,Visual Studio 2010,Visual C++,我在Wow64DisableWow64FsRedirection()中遇到问题 当我在Visual Studio 2010中为64位平台编译时,它在窗口10中不工作,返回错误1 但是,当我为32位平台编译时,该函数在windows10中工作 typedef BOOL(__stdcall *tFSRED)(HMODULE ); HMODULE hKernel = LoadLibrary(_T("Kernel32.dll")); // Get Kernel module handle if (hK

我在
Wow64DisableWow64FsRedirection()
中遇到问题

当我在Visual Studio 2010中为64位平台编译时,它在窗口10中不工作,返回错误1

但是,当我为32位平台编译时,该函数在windows10中工作

typedef BOOL(__stdcall *tFSRED)(HMODULE );

HMODULE hKernel = LoadLibrary(_T("Kernel32.dll")); // Get Kernel module handle
if (hKernel == NULL)
{
    return 2;
}

tFSRED pFunc;
pFunc = (tFSRED) GetProcAddress(hKernel, "Wow64DisableWow64FsRedirection");
ret = pFunc(&oldValue);  // Turn the file the file system redirector off
if (ret == FALSE)
{
    _tprintf(_T("\nFile System Redirection could not be turned off. Reason: %d"), GetLastError());
    return 4;
}
// ret is always TRUE when compile through 32-bit platform
// but ret return FALSE when compile through 64-bit platform
在Visual Studio中,如果我为64位平台编译,那么在编译之后,如果我在
depends.EXE
中打开生成的EXE,它会在EXE前面显示64


选择32位平台后,生成的EXE为32位。在我的例子中,该EXE可以工作,但我希望我的EXE是64位的,因此我选择64位平台,我得到一个64位的EXE,但它不能像我上面解释的那样工作。

WOW64仿真仅适用于在64位系统上运行的32位可执行文件。64位EXE不应尝试使用WOW64函数(除了
IsWow64Process()
),因为EXE一开始不会在WOW64内部运行。这就是为什么在64位EXE中出现错误代码1(
error\u INVALID\u FUNCTION

因此,要么:

  • ifdef在64位编译中跳过WOW64函数的代码:

    #ifndef _WIN64
    typedef BOOL (WINAPI *tFSDisable)(PVOID*);
    typedef BOOL (WINAPI *tFSRevert(PVOID);
    
    HMODULE hKernel = GetModuleHandle(_T("Kernel32"));
    tFSDisable pDisableFunc = (tFSDisable) GetProcAddress(hKernel, "Wow64DisableWow64FsRedirection");
    tFSRevert pRevertFunc = (tFSRevert) GetProcAddress(hKernel, "Wow64RevertWow64FsRedirection");
    
    PVOID oldValue;
    
    if ((pDisableFunc) && (pRevertFunc))
    {
        if (!pDisableFunc(&oldValue))  // Turn off the file system redirector
        {
            _tprintf(_T("\nFile System Redirection could not be turned off. Reason: %d"), GetLastError());
            return 4;
        }
    }
    #endif
    
    // do file system operations as needed...
    
    #ifndef _WIN64
    if ((pDisableFunc) && (pRevertFunc))
    {
        if (!pRevertFunc(oldValue))  // Restore the file system redirector
        {
            _tprintf(_T("\nFile System Redirection could not be restored. Reason: %d"), GetLastError());
            return 5;
        }
    }
    #endif
    
  • 省略ifdef,仅当
    IsWow64Process()
    报告当前进程实际上正在WOW64内部运行时才调用WOW64函数:

    typedef BOOL (WINAPI tW64P)(HANDLE, PBOOL);
    typedef BOOL (WINAPI *tFSDisable)(PVOID*);
    typedef BOOL (WINAPI *tFSRevert(PVOID);
    
    HMODULE hKernel = GetModuleHandle(_T("Kernel32"));
    
    tW64P pIsWow64Func = (tW64P) GetProcAddress(hKernel, "IsWow64Process");
    tFSDisable pDisableFunc = NULL;
    tFSRevert pRevertFunc = NULL;
    
    BOOL bIsWow64 = FALSE;
    if (pIsWow64Func)
    {
        pIsWow64Func(GetCurrentProcess(), &bIsWow64);
        if (bIsWow64)
        {
            pDisableFunc = (tFSDisable) GetProcAddress(hKernel, "Wow64DisableWow64FsRedirection");
            pRevertFunc = (tFSRevert) GetProcAddress(hKernel, "Wow64RevertWow64FsRedirection");
        }
    }
    
    PVOID oldValue;
    
    if ((pDisableFunc) && (pRevertFunc))
    {
        if (!pDisableFunc(&oldValue))  // Turn off the file system redirector
        {
            _tprintf(_T("\nFile System Redirection could not be turned off. Reason: %d"), GetLastError());
            return 4;
        }
    }
    
    // do file system operations as needed...
    
    if ((pDisableFunc) && (pRevertFunc))
    {
        if (!pRevertFunc(oldValue))  // Restore the file system redirector
        {
            _tprintf(_T("\nFile System Redirection could not be restored. Reason: %d"), GetLastError());
            return 5;
        }
    }
    

将代码包含在问题正文中,以便您可以将其正确格式化为代码。