Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wow64DisableWow64FsRedirection fileExits

a file is located in C:\program files (x86)\my app\myexe.exe

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

  2. FileExists('C:\program files\my app\myexe.exe') returns false; in both cases, if I use Wow64DisableWow64FsRedirection or not.

Why ? Thanks

like image 339
user382591 Avatar asked Dec 10 '25 20:12

user382591


1 Answers

File system redirection is only there for the %windir%\system32 directory. The description of the File System Redirector seems to make this obvious.

Note the comment in the page

Applications should use the SHGetSpecialFolderPath function to determine the %ProgramFiles% directory name.

Edit Turns out that the FOLDERID_ProgramFilesx64 does not work on 32bit applications running on 64bit windows. In this case, you can use the environment variable %ProgramW6432% instead. Note that this variable is only available on Windows 7 and later for 32bit applications.

The following delphi snippet allows accessing the variable:

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;

Called as:

GetEnvironmentString('%ProgramW6432%');

IF you're on a 64bit version of windows, then a 32bit application cannot use FOLDERID_ProgramFilesX64 to explicitly get the 64bit location of Program Files, but can use the environment variable expansion instead. On a 32bit version of windows, this location is invalid, and will not get you a value. You need to check the bitness of the system before attempting to access this variable.

You can use the function IsWow64Process to determine this. The following snippet should allow you to check this:

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;

In summary: FOLDERID_ProgramFiles gives you the 32/64 bit variant when accessed from a 32/64 bit program, FOLDERID_ProgramFilesX64 gives you the 64bit version explicitly on a 64-bit application, and FOLDERID_ProgramFilesX86 gives you the 32bit variant explicitly. You can use the environment variable expansion to get the 64bit value on a 32bit application

like image 193
Petesh Avatar answered Dec 12 '25 19:12

Petesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!