6

我正在尝试通过调用 CopyFileA 将文件复制到 C:\windows\system32 - 调试显示字符串“C:\windows\system32\filename”确实被发送到 CopyFileA,但我的文件被复制到“C:\ windows\system32\sysWOW64\filename" 代替。有谁知道为什么会这样?

4

3 回答 3

5

在 64 位 Windows 上,Windows 为 32 位进程执行文件系统重定向。要禁用,请调用Wow64DisableWow64FsRedirection

为了让应用也能在 32 位 Windows XP 上运行,Wow64DisableWow64FsRedirection 必须在运行时动态链接。这是我使用的代码:

BOOL DisableWow64FsRedirection(PVOID* OldValue)
{
#ifdef WIN64
    UNREFERENCED_PARAMETER(OldValue);
    return TRUE;
#else
    typedef BOOL (WINAPI * LPWOW64DISABLEWOW64FSREDIRECTION)(PVOID *);

    LPWOW64DISABLEWOW64FSREDIRECTION    fnWow64DisableWow64FsRedirection;
    HMODULE                             kernelMod;
    BOOL                                success = TRUE;

    kernelMod = GetModuleHandleW(L"kernel32");
    if (kernelMod)
    {
        fnWow64DisableWow64FsRedirection = (LPWOW64DISABLEWOW64FSREDIRECTION)GetProcAddress(kernelMod, "Wow64DisableWow64FsRedirection");
        if (fnWow64DisableWow64FsRedirection)
            success = fnWow64DisableWow64FsRedirection(OldValue);
    }

    return success;
#endif
}

BOOL RevertWow64FsRedirection(PVOID OldValue)
{
#ifdef WIN64
    UNREFERENCED_PARAMETER(OldValue);
    return TRUE;
#else
    typedef BOOL (WINAPI * LPWOW64REVERTWOW64FSREDIRECTION)(PVOID);

    LPWOW64REVERTWOW64FSREDIRECTION fnWow64RevertWow64FsRedirection;
    HMODULE                         kernelMod;
    BOOL                            success = TRUE;

    kernelMod = GetModuleHandleW(L"kernel32");
    if (kernelMod)
    {
        fnWow64RevertWow64FsRedirection = (LPWOW64REVERTWOW64FSREDIRECTION)GetProcAddress(kernelMod, "Wow64RevertWow64FsRedirection");
        if (fnWow64RevertWow64FsRedirection)
            success = fnWow64RevertWow64FsRedirection(OldValue);
    }

    return success;
#endif
}
于 2011-08-30T22:50:36.347 回答
2

简单地说,如果你想检查操作系统然后检查它并访问文件夹 system32 就像:

string os = Environment.GetEnvironmentVariable("WINDIR") + "\\SysWOW64";
            if (Directory.Exists(os))
            {
                destinationDir = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative\\");
        }

这样,您可以将文件复制到 system32 文件夹中。

享受:阿里·拉扎

于 2012-03-29T09:21:59.110 回答
1

这个解决方案对我来说似乎更好:感谢 Bevan Collins。

BOOL changeWow64Redirection(PVOID& param, BOOL toDisable = true)
{
#ifdef WIN64
  UNREFERENCED_PARAMETER(OldValue);
  return TRUE;
#else
  typedef BOOL (WINAPI * LPWOW64CHANGEWOW64FSREDIRECTION)(PVOID *);
  LPWOW64CHANGEWOW64FSREDIRECTION     fnWow64ChangeWow64FsRedirection;
  HMODULE                             kernelMod;
  BOOL                                success = FALSE;
  kernelMod = GetModuleHandle(_T("kernel32"));
  if (kernelMod)
  {
    if (toDisable)
      fnWow64ChangeWow64FsRedirection = (LPWOW64CHANGEWOW64FSREDIRECTION)GetProcAddress(kernelMod, "Wow64DisableWow64FsRedirection");
    else
      fnWow64ChangeWow64FsRedirection = (LPWOW64CHANGEWOW64FSREDIRECTION)GetProcAddress(kernelMod, "Wow64RevertWow64FsRedirection");
    if (fnWow64ChangeWow64FsRedirection)
      success = fnWow64ChangeWow64FsRedirection(&param);
  }
  return success;
#endif //WIN64
}
于 2013-12-24T12:41:15.823 回答