Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

delphi - Could not find system file when it actually exists

System can't find alg.exe but it does exist - "c:windowssystem32alg.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:windowssystem32alg.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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:windowssystem32alg.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:windowsSysNativealg.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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...