Could not find system file when it actually exists
You have a 32 bit process running on a 64 bit system. Your process runs inside the WOW64 emulator and is subject to the File System Redirector. This redirects references to the 64 bit system folder, %windir%\System32, to the 32 bit system folder, %windir%\SysWOW64.
The recommended way to gain access to the 64 bit system folder, from within a 32 bit process running under the emulator is to use the %windir%\Sysnative alias:
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.
Naturally, this alias only exists on 64 bit systems. The simplest way to check that is to test the value of TOSVersion.Architecture
.
Whilst it is possible to disable the redirector, it is not advisable to do so. The documentation says:
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.
I would strongly advise you against disabling the file system redirector.
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 Wow64DisableWow64FsRedirection
function 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.