Could not find system file when it actually exists

This is because the WOW64 file system redirection, if your 32-bit app that want to gain access to the native system32 directory, you must use the Wow64DisableWow64FsRedirectionfunction or the Sysnative Alias.

Wow64DisableWow64FsRedirection

Try this sample

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows;

Function Wow64DisableWow64FsRedirection(Var Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
  External 'Kernel32.dll' Name 'Wow64DisableWow64FsRedirection';
Function Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection: LongBool): LongBool; StdCall;
  External 'Kernel32.dll' Name 'Wow64EnableWow64FsRedirection';

Var
  Wow64FsEnableRedirection: LongBool;
begin
 try
    if Wow64DisableWow64FsRedirection(Wow64FsEnableRedirection) then
    begin
      if FileExists('c:\windows\system32\alg.exe') then
       Writeln('fe')
      else
       Writeln('fne');

      if not Wow64EnableWow64FsRedirection(Wow64FsEnableRedirection) then
       RaiseLastOSError;
    end
    else
    RaiseLastOSError;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;

end.
end.

Additionally check the MSDN documentation for this topic.

Applications can control the WOW64 file system redirector using the
Wow64DisableWow64FsRedirection, Wow64EnableWow64FsRedirection, and
Wow64RevertWow64FsRedirection functions. Disabling file system
redirection affects all file operations performed by the calling
thread, so it should be disabled only when necessary for a single
CreateFile call and re-enabled again immediately after the function
returns. Disabling file system redirection for longer periods can
prevent 32-bit applications from loading system DLLs, causing the
applications to fail.

Sysnative

32-bit applications can access the native system directory by
substituting %windir%\Sysnative for %windir%\System32. WOW64
recognizes Sysnative as a special alias used to indicate that the file
system should not redirect the access. This mechanism is flexible and
easy to use, therefore, it is the recommended mechanism to bypass file
system redirection. Note that 64-bit applications cannot use the
Sysnative alias as it is a virtual directory not a real one.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  Windows;

begin
 try
    if FileExists('c:\windows\SysNative\alg.exe') then
     Writeln('fe')
    else
     Writeln('fne');
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;

end.

Leave a Comment