gpt4 book ai didi

c++ - 多次调用 CreateFileA 时为 INVALID_HANDLE_VALUE

转载 作者:搜寻专家 更新时间:2023-10-31 00:34:30 29 4
gpt4 key购买 nike

我正在使用 CreateFileA,我第一次调用它时,它按预期工作。但是当我第二次调用它时,它返回句柄 INVALID_HANDLE_VALUE。可能是什么问题呢?仅供引用,每次我需要检查我的 USB 设备是否已连接时我都会调用它..

int port = 500;
char port_name [MAX_CAR] = {0};

sprintf_s (port_name, MAX_CAR, "\\\\.\\COM%d", port);

com->id = CreateFileA (port_name,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);

编辑:我确实尝试像 CloseHandle(com->id); 这样使用 CloseHandle;但这没有帮助。

最佳答案

来自documentation :

When an application is finished using the object handle returned by CreateFile, use the CloseHandle function to close the handle. This not only frees up system resources, but can have wider influence on things like sharing the file or device and committing data to disk. Specifics are noted within this topic as appropriate.

关于c++ - 多次调用 CreateFileA 时为 INVALID_HANDLE_VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934952/

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