Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find system file when it actually exists

Tags:

delphi

System can't find alg.exe but it does exist - "c:\windows\system32\alg.exe".

I've moved from Win 7 x86 to x64 recently and when I was on x86 I had no problem with this, tried Delphi 7 & XE2.

Code I'm using:

if FileExists('c:\windows\system32\alg.exe') then
  ShowMessage('fe') else ShowMessage('fne');

Tried to take ownership of a file + my app with admin privilegies - same issue.

Guys, to check if x64..

function IsWow64Process(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall; external 'kernel32.dll';

function IS64 : Boolean;
var
 xIS64 : Bool;
begin
 if IsWow64Process(GetCurrentProcess, xIS64) then
  Result := xIS64 else RaiseLastOSError;
end;
like image 909
user2200585 Avatar asked Mar 24 '13 19:03

user2200585


2 Answers

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.
like image 139
RRUZ Avatar answered Oct 05 '22 23:10

RRUZ


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.

like image 32
David Heffernan Avatar answered Oct 05 '22 23:10

David Heffernan