Wow64DisableWow64FsRedirection() 函数在 Window 10 中的 Visual Studio 中编译为 64 位时不起作用
Wow64DisableWow64FsRedirection() function not working when compiled for 64-bit in Visual Studio in Window 10
我遇到了 Wow64DisableWow64FsRedirection()
的问题。
当我在 Visual Studio 2010 中为 64 位平台编译时,它在 Window 10 中不工作,它 returns 错误 1。
但是,当我为 32 位平台编译时,该函数在 Window 10.
中有效
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_INVALID_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;
}
}
我遇到了 Wow64DisableWow64FsRedirection()
的问题。
当我在 Visual Studio 2010 中为 64 位平台编译时,它在 Window 10 中不工作,它 returns 错误 1。
但是,当我为 32 位平台编译时,该函数在 Window 10.
中有效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_INVALID_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; } }