我有一个使用 CreateFile() API 读取/写入 USB 设备的库。 该设备恰好实现了 HID 设备配置文件,因此它与 Microsoft 的 HID 类驱动程序兼容。
系统上安装的其他一些应用程序正在以读/写模式打开设备,而没有共享模式。 这会阻止我的库(以及使用它的任何内容)使用该设备。 我想这就是 HID 兼容设备的问题所在——其他驱动程序软件(鼠标、控制器、PHIDGETS 等)可能不合作。
无论如何,设备文件路径的形式为:
1: "\\?\hid#hpqremhiddevice&col01#5&21ff20e7&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
2: "\\?\hid#vid_045e&pid_0023#7&34aa9ece&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
3: "\?\hid#vid_056a&pid_00b0&col01#6&5b05f29&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
我正在尝试使用代码打开它,例如:
// First, open it with minimum permissions, this device may not be ours.
// we'll re-open it later in read/write
hid_device_ref = CreateFile(
device_path, GENERIC_READ,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
我考虑过使用 SysInternals 的 FileMon 或 Process Monitor 等工具。 但我似乎无法让它报告设备文件句柄的使用情况,如上面列出的那样。
I have a library that reads/writes to a USB-device using CreateFile() API. The device happens to implement the HID-device profile, such that it's compatible with Microsoft's HID class driver.
Some other application installed on the system is opening the device in read/write mode with no share mode. Which prevents my library (and anything that consumes it) from working with the device. I suppose that's the rub with being an HID-compatible device -- other driver software (mice, controllers, PHIDGETS, etc) can be uncooperative.
Anyway, the device file path is of the form:
1: "\\?\hid#hpqremhiddevice&col01#5&21ff20e7&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
2: "\\?\hid#vid_045e&pid_0023#7&34aa9ece&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
3: "\?\hid#vid_056a&pid_00b0&col01#6&5b05f29&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}".
And I'm trying to open it using code, like:
// First, open it with minimum permissions, this device may not be ours.
// we'll re-open it later in read/write
hid_device_ref = CreateFile(
device_path, GENERIC_READ,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
I've considered a tool like FileMon or Process Monitor from SysInternals. But I can't seem to get it to report usage on device file handles like the one listed above.
发布评论
评论(4)
您可以使用一个技巧,打开设备句柄,既不请求读取也不请求写入权限,并仅使用功能报告与其进行交互。 Jan Axelson 在她关于 USB HID 设备的书中提到了这个技巧。 我相信这可以解决排它锁的问题,例如,当您尝试打开 Windows 认为是系统键盘或鼠标的设备的句柄时,您会遇到该问题。 即使您无法读取或写入句柄,您仍然可以使用
HidD_SetFeature
并使用HidD_GetFeature
。 我不知道在这种情况下读取输入报告或发送输出报告的方法,也许这是不可能的,但您可能不需要其中任何一个,特别是如果设备在某种意义上是“您的”设备您控制固件。 严格来说,这并不能回答你提出的问题,但它似乎可能相关,所以我想我会把它扔在那里。There's a trick you can do where you open the device handle requesting neither read nor write permission and interact with it using only feature reports. Jan Axelson mentions this trick in her books about USB HID devices. I believe this gets around the problem with the exclusive lock, which you would encounter (for example) when trying to open a handle to a device that Windows considers a system keyboard or mouse. Even though you can't read or write the handle, you can still send a feature report to the device using
HidD_SetFeature
and read a report from the device usingHidD_GetFeature
. I don't know offhand of a way to read input reports or send output reports under these circumstances, and perhaps it's impossible to do so, but you might not need either of those, especially if the device is "your" device in the sense that you control the firmware. Strictly speaking this does nothing to answer your question as asked, but it seemed potentially relevant so I figured I'd throw it out there.您是否尝试过 sysinternals 中名为 handle 的工具?
无论如何,Windows 都不会这样做(显示锁定设备的应用程序的名称):当您尝试弹出 USB 设备时,Windows 只是说该设备当前正在使用,现在无法删除。
Have you tried the tool called handle from sysinternals?
Anyway, neither windows does this (display the name of the application that locked the device): when you try to eject an USB device, Windows just says that the device is currently in use and cannot be remove right now.
这就是我用来从 Magtek 读卡器读取数据的方法:
尝试这些选项,看看是否至少可以从设备读取数据。
我理解您的痛苦...我发现 USB HID 文档在几个地方基本上是错误的。
[编辑] 关于这个问题目前还没有太多的内容。 这是代码项目链接,在底部的线程中轻轻触及主题。 听起来好像如果是键盘或鼠标窗口可能会独占它。
This is what I use to read from a Magtek card reader:
Try those options and see if you can at least read from the device.
I understand your pain here... I found the USB HID documentation to be basically wrong in several places.
[Edit] There's not much out there on this problem. Here's a codeproject link that lightly touches on the subject in a thread at the bottom. Sounds like maybe if it's a keyboard or mouse windows grabs it exclusively.
酷 - 我会尝试这些选项,因为考虑到我的意图,它们可能是更好的默认值。 不幸的是,我知道我的设备在那里,我最终将需要读/写访问权限(一旦我检查描述符并验证它实际上是我的设备)。
这意味着我的真正目标是知道什么在使用它,因此我可以通知客户/用户:“嘿,伙计,‘iexplore.exe’当前正在使用您的 SuperWidget 设备。您必须将其关闭才能使用SuperWidget 应用程序。” (如果不是在应用程序级别,那么至少在电话支持级别。)
我忘了提及 GetLastError() 报告的 Windows 错误是:
(因此,假设没有 FILE_SHARE_NONE 代表其他进程,您的共享替代方案可能会打开文件)。
[编辑]
Cool - I'll try those options, as they're probably better defaults given my intentions. Unfortunately, I know my device is there and I'll eventually need read/write access later on (once I inspect the descriptors and have verifed it is infact my device).
Which means that my real goal IS to know what's using it, so I can inform the customer/user: "Hey man, 'iexplore.exe' is currently using your SuperWidget device. You'll have to close that down in order to use SuperWidget application." (if not at the application-level, then at least at the phone support level.)
I forgot to mention that the windows error reported by GetLastError() is:
(So your sharing alternatives will probably get the file open, assuming no FILE_SHARE_NONE on behalf of the other process).
[edit]