简体   繁体   English

WINAPI:文件存在检查失败

[英]WINAPI: File exists check fail

I want to check if a certain file exists in the specified location. 我想检查指定位置是否存在某个文件。 I have been trying multiple solutions for that but seem like none of them work properly, since all of them return false. 我一直在尝试多种解决方案,但是似乎所有解决方案均无法正常工作,因为所有解决方案均返回false。

There is no doubt that the file exists in the specified location. 毫无疑问,该文件存在于指定位置。

Executable is being run as administrator, so I'm having the appropriate permissions. 可执行文件以管理员身份运行,因此我具有适当的权限。

Code I used: 我使用的代码:

#include <io.h>
#include <string>
#include <Shlwapi.h>

std::string str = "C:\WINDOWS\System32\iluminated.dll";
unsigned long attrib = GetFileAttributes(str.c_str());

bool exists1 = (attrib != INVALID_FILE_ATTRIBUTES && 
            !(attrib & FILE_ATTRIBUTE_DIRECTORY)) &&
            GetLastError() != ERROR_FILE_NOT_FOUND; // false
bool exists2 = ( _access( str.c_str(), 0 ) != -1 ); // false
bool exists3 = PathFileExists(str.c_str()) != 0; // false

Is there anything I'm doing wrong? 我做错了什么吗?

You should use double back slashes for paths, since if you use single back slashes in a string they are interpreted as command symbols (line \\n , for example): 您应该对路径使用双反斜杠,因为如果在字符串中使用单反斜杠,它们将被解释为命令符号(例如, \\n行):

"C:\\WINDOWS\\System32\\iluminated.dll"

Alternatively, you can use forward slashes, they work on most operating systems: 另外,您可以使用正斜杠,它们可在大多数操作系统上使用:

"C:/WINDOWS/System32/iluminated.dll"

I found the answer. 我找到了答案。 Turns out Windows is always redirecting system32 to syswow64 while trying to access in 64-bit Windows. 事实证明,Windows总是在尝试在64位Windows中进行访问时将system32重定向到syswow64 I had to use SysNative directory, even though it doesnt exist - Windows redirects it to the proper system32 directory. 我必须使用SysNative目录,即使它不存在-Windows也会将其重定向到正确的system32目录。

Since Visual Studio 2012, application projects default to “Any CPU 32-bit preferred”. 从Visual Studio 2012开始,应用程序项目默认为“首选32位CPU”。 If you run such an executable on a 64-bit Windows operating system, then it will start as a 32-bit process and be affected by WOW64 file system redirection. 如果在64位Windows操作系统上运行这样的可执行文件,则它将作为32位进程启动,并受到WOW64文件系统重定向的影响。

When a 32-bit process on 64-bit Windows tries to access "C:\\Windows\\System32", WOW64 redirects it to "C:\\Windows\\SysWOW64". 当64位Windows上的32位进程尝试访问“ C:\\ Windows \\ System32”时,WOW64将其重定向到“ C:\\ Windows \\ SysWOW64”。 There are several ways to access the real "C:\\Windows\\System32" directory: 有几种方法可以访问真实的“ C:\\ Windows \\ System32”目录:

  • Use "C:\\Windows\\SysNative", which WOW64 redirects to "C:\\Windows\\System32" even though it does not appear in directory listings. 使用“ C:\\ Windows \\ SysNative”,即使它未出现在目录列表中,WOW64也会将其重定向到“ C:\\ Windows \\ System32”。 This is an easy way and unlikely to cause problems. 这是一种简单的方法,不会引起问题。
  • Use Wow64DisableWow64FsRedirection and Wow64RevertWow64FsRedirection. 使用Wow64DisableWow64FsRedirection和Wow64RevertWow64FsRedirection。
  • Use a 64-bit process. 使用64位进程。

Source: https://social.msdn.microsoft.com/Forums/en-US/c54f8368-035e-478e-b988-b180a3c7e3da/file-not-found-for-existing-file-in-system32-directory?forum=csharpgeneral 来源: https : //social.msdn.microsoft.com/Forums/en-US/c54f8368-035e-478e-b988-b180a3c7e3da/file-not-found-for-existing-file-in-system32-directory?forum=通用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM