gpt4 book ai didi

CreateFileA 失败并显示 ERROR_ACCESS_DENIED

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:36 30 4
gpt4 key购买 nike

我需要检索目录句柄才能调用 ReadDirectoryChangesW 就可以了。实际上我需要的不止于此,但让我们先放轻松。我已将问题缩小为:

m_directoryHandle = CreateFileA(
"C:\\Users\\victor\\Documents\\Projets\\libxnotify\\unittests", // __in LPCTSTR lpFileName,
FILE_LIST_DIRECTORY, // __in DWORD dwDesiredAccess,
0, // __in DWORD dwShareMode,
0, // __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
OPEN_EXISTING, // __in DWORD dwCreationDisposition,
0, // __in DWORD dwFlagsAndAttributes,
0 // __in_opt HANDLE hTemplateFile
);

这将返回一个 INVALID_HANDLE_VALUE,最后一个错误代码为 ERROR_ACCESS_DENIED。不用说,我尝试了很多不同的参数,但没有一个起作用。我以 victor 身份运行我的程序,并确保我确实拥有该 unittests 目录的权限,我打开了一个命令 shell 并键入:

C:\Users\victor>echo bla >> "C:\Users\victor\Documents\Projets\libxnotify\unittests\test"

它奏效了。

最佳答案

ReadDirectoryChangesW 的文档有一句话说:

To obtain a handle to a directory, use the CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag.

CreateFile 的文档然后对此还有更详细的评论:

Directories

An application cannot create a directory by using CreateFile, therefore only the OPEN_EXISTING value is valid for dwCreationDisposition for this use case. To create a directory, the application must call CreateDirectory or CreateDirectoryEx.

To open a directory using CreateFile, specify the FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes. Appropriate security checks still apply when this flag is used without SE_BACKUP_NAME and SE_RESTORE_NAME privileges.

您错过了 dwFlagsAndAttributes 的这个重要的 FILE_FLAG_BACKUP_SEMANTICS 标志。

关于CreateFileA 失败并显示 ERROR_ACCESS_DENIED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559696/

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