在默认情况下,64位环境运行32位程序,会启用重定向,比如调用CreateFile时,系统会把system32文件夹重定向到Syswow64等等。但是有些时候需要访问system32文件夹的时候就需要关闭重定向。MS已经提供了一组函数用来控制重定向: Wow64EnableWow64FsRedirection,Wow64DisableWow64FsRedirection,Wow64RevertWow64FsRedirection
用法在MSDN里面有DEMO:
#define _WIN32_WINNT 0x0501
#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
}
}
换成AU3代码,一句就搞定了 #RequireAdmin
DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1) ; 默认情况下,禁用32位应用程序被重定向到syswow64而不是system32 ;
|