Wow64DisableWow64FsRedirection fileExits

时间:2012-07-19 07:26:51

标签: windows delphi delphi-7 delphi-2010

文件位于C:\ program files(x86)\ my app \ myexe.exe

  1. FileExists('C:\ program files(x86)\ my app \ myexe.exe')返回 true ;

  2. FileExists('C:\ program files \ my app \ myexe.exe')返回 false ; 如果我使用Wow64DisableWow64FsRedirection,则两种情况

  3. 为什么?感谢

1 个答案:

答案 0 :(得分:8)

文件系统重定向仅适用于%windir%\system32目录。 description of the File System Redirector似乎使这一点显而易见。

请注意页面中的评论

  

应用程序应使用SHGetSpecialFolderPath函数来确定%ProgramFiles%目录名。

编辑结果证明FOLDERID_ProgramFilesx64不适用于在64位窗口上运行的32位应用程序。在这种情况下,您可以使用环境变量%ProgramW6432%。请注意,此变量在Windows 7及更高版本的32位应用程序中仅

以下delphi代码段允许访问变量:

function GetEnvironmentString(aString : string) : string;
var
  dest : string;
  retSize : integer;
begin
  SetLength(dest, MAX_PATH);
  retSize := ExpandEnvironmentStrings(pchar(aString), pchar(dest), MAX_PATH);
  if retSize > 0 then
      SetLength(dest, retSize - 1);
  result := dest;
end;

被称为:

GetEnvironmentString('%ProgramW6432%');

如果您使用的是64位版本的Windows,那么32位应用程序无法使用FOLDERID_ProgramFilesX64显式获取Program Files的64位位置,但可以使用环境变量扩展。在32位版本的Windows上,此位置无效,并且不会为您提供值。在尝试访问此变量之前,您需要检查系统的位数。

您可以使用函数IsWow64Process来确定这一点。 following snippet应该允许您检查:

function IsWow64: Boolean;
type
  TIsWow64Process = function(Handle: Windows.THandle; var Res: Windows.BOOL): Windows.BOOL; stdcall;
var
  IsWow64Result: Windows.BOOL;
  IsWow64Process: TIsWow64Process;
begin
  // Try to load required function from kernel32
  IsWow64Process := Windows.GetProcAddress(Windows.GetModuleHandle('kernel32.dll'), 'IsWow64Process');
  if Assigned(IsWow64Process) then
  begin
    // Function is implemented: call it
    if not IsWow64Process(Windows.GetCurrentProcess, IsWow64Result) then
      raise SysUtils.Exception.Create('IsWow64: bad process handle');
    // Return result of function
    Result := IsWow64Result;
  end
  else
    // Function not implemented: can't be running on Wow64
    Result := False;
end;

总结FOLDERID_ProgramFiles在从32/64位程序访问时为您提供32/64位变体,FOLDERID_ProgramFilesX64在64位显式提供64位版本-bit应用程序,FOLDERID_ProgramFilesX86明确地为您提供32位变体。您可以使用环境变量扩展来获取32位应用程序上的64位值