gpt4 book ai didi

windows - 是否存在函数 CreateFile 返回 INVALID_HANDLE_VALUE 而 GetLastError() 返回 ERROR_ALREADY_EXISTS 的情况

转载 作者:可可西里 更新时间:2023-11-01 11:48:09 25 4
gpt4 key购买 nike

我在MSDN上搜索了一下

HANDLE WINAPI CreateFile(
_In_ LPCTSTR lpFileName,
_In_ DWORD dwDesiredAccess,
_In_ DWORD dwShareMode,
_In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
_In_ DWORD dwCreationDisposition,
_In_ DWORD dwFlagsAndAttributes,
_In_opt_ HANDLE hTemplateFile
);

如果 dwCreationDisposition == CREATE_ALWAYSOPEN_ALWAYS,它表示

Creates a new file, always. If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183). If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero. For more information, see the Remarks section of this topic.

Opens a file, always. If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183). If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.

所以我不确定如果函数失败,GetLastError() 会是 ERROR_ALREADY_EXISTS 吗?

如果是,请给我一个例子。

非常感谢。

最佳答案

快速回答:否。

根据规定:

ERROR_ALREADY_EXISTS 应在 CreateFile() 调用之后由 GetLastError() 返回,仅当文件存在,dwCreationDisposition 设置为 CREATE_ALWAYSOPEN_ALWAYS 并且 CreateFile() 成功 .

如果 CreateFile() 失败,CREATE_ALWAYSOPEN_ALWAYS 设置为 dwCreationDisposition,则 GetLastError( ) 应返回适当的最后错误代码,指示无法打开文件的原因,但不能是 ERROR_ALREADY_EXISTS 因为用户特别请求打开或创建文件,无论是否是否存在。

当调用 CreateFile() 并将 dwCreationDisposition 设置为 时,这不应与 GetLastError() 的返回值混淆>CREATE_NEW 失败,因为文件存在。在这种情况下,GetLastError() 应返回 ERROR_FILE_EXISTS 并且从不 ERROR_ALREADY_EXISTS

关于windows - 是否存在函数 CreateFile 返回 INVALID_HANDLE_VALUE 而 GetLastError() 返回 ERROR_ALREADY_EXISTS 的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38784825/

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