gpt4 book ai didi

delphi - 当系统文件实际存在时找不到它

转载 作者:行者123 更新时间:2023-12-03 14:38:12 24 4
gpt4 key购买 nike

系统找不到alg.exe,但它确实存在 - “c:\windows\system32\alg.exe”。

我最近从 Win 7 x86 迁移到 x64,当我使用 x86 时,我对此没有任何问题,尝试了 Delphi 7 和 XE2。

我正在使用的代码:

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

尝试使用管理员权限获取文件+我的应用程序的所有权 - 同样的问题。

伙计们,检查一下是否是 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;

最佳答案

这是因为WOW64 file system redirection ,如果您的32位应用程序想要访问 native system32目录,则必须使用Wow64DisableWow64FsRedirection函数或 Sysnative 别名。

Wow64禁用Wow64Fs重定向

尝试这个示例

{$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.

另外,请查看此主题的 MSDN 文档。

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.

关于delphi - 当系统文件实际存在时找不到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com