如果未使用 CloseHandle 正确关闭,则重新打开串行端口失败

2022-01-18 00:00:00 windows serial-port c++ createfile

我在 Windows 上使用被视为虚拟串行端口的 USB 设备.我可以使用 CreateFile 和 ReadFile 函数与设备通信,但在某些情况下,我的应用程序不会调用 CloseHandle(当我的开发应用程序崩溃时).之后,对 CreateFile 的所有调用都失败(ERROR_ACCESS_DENIED),唯一的解决方案是再次登录我的计算机.有没有办法以编程方式强制关闭打开的句柄(或重新打开)?

I am working with USB device on Windows that is seen as a virtual serial port. I can communicate with the device using CreateFile and ReadFile functions, but in some cases my application does not call CloseHandle (when my application in development crashes). After that all calls to CreateFile fail (ERROR_ACCESS_DENIED), and the only solution is to log in to my computer again. Is there any way to force closing the open handle (or reopening) programmatically?

推荐答案

这当然不正常.在进程终止后,Windows 会自动关闭所有打开的句柄.这一定是您的 USB 设备驱动程序中的一个缺陷,尽管很难看出它是如何搞砸的.然而,模拟串行端口的那些是出了名的糟糕.好吧,除了希望制造商更新驱动程序之外,您无能为力.或者其他制造商的设备.

This is certainly not normal. Windows automatically closes any handles that are left open after the process terminates. This must be a flaw in your USB device driver, although it is hard to see how it could mess this up. The ones that emulate serial ports are however notoriously lousy. Well, nothing much you can do but hope for a driver update from the manufacturer. Or a device from another manufacturer.

相关文章