如何使用 Windows API 重置 USB 设备? [英] How do I reset USB devices using the Windows API?

查看:95
本文介绍了如何使用 Windows API 重置 USB 设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道使用 Windows XP API 重置 USB 总线的方法吗?换句话说,我希望操作系统踢出当前连接的所有 USB 设备,然后重新自动检测所有内容.

Do you know a way to use the Windows XP API to reset the USB bus? In other words, I'd like the OS to kick out any USB devices that are currently connected, and then auto-detect everything anew.

我知道 devcon,我想我可以对它进行系统调用,但我希望直接调用 API.

I'm aware of devcon, and I suppose I could do system calls out to it, but I'm hoping for a direct call into the API.

推荐答案

从内核模式:您可以强制重新连接特定的 USB 设备,就像拔掉并重新插入一样,通过向其 PDO 发送 IOCTL_INTERNAL_USB_CYCLE_PORT.(这只能从内核模式完成,例如通过辅助驱动程序.)这个循环"操作将导致 USB 重置发生,之后设备将被重新枚举.例如,如果设备返回不同的 USB 设备描述符,则可能会为其匹配不同的驱动程序.

From kernel mode: You can force a specific USB device to be re-connected, as if it was unplugged and replugged again, by sending an IOCTL_INTERNAL_USB_CYCLE_PORT to its PDO. (This can only be done from a kernel mode, e.g. through a helper driver.) This 'cycle' operation will cause a USB reset to occur, after which the device would be re-enumerated. For example, if the device comes back with a different USB device descriptor, a different driver may be matched for it.

从用户模式:您可以通过 CfgMgr API 弹出设备来执行此操作.例如,要检查所有 USB 集线器并弹出所有设备:

From user mode: You can do this by ejecting the device through the CfgMgr API. For example, to go over all USB hubs and eject all devices:

  1. 使用 SetupDiGetClassDevs(... DEVICEINTERFACE) 查找具有设备接口 GUID_DEVINTERFACE_USB_HUB 的所有设备.
  2. 枚举返回的设备信息集 (SetupDiEnumDeviceInfo).
  3. 对于每个设备,获取 DevInst 成员:
  1. Find all devices having device interface GUID_DEVINTERFACE_USB_HUB with SetupDiGetClassDevs(... DIGCF_DEVICEINTERFACE).
  2. Enumerate over the returned device information set (SetupDiEnumDeviceInfo).
  3. For each device, get the DevInst member:
  1. 重复调用 CM_Get_Child(DevInst)CM_Get_Sibling 以遍历集线器的所有子节点(即 USB 设备).
  2. 对于每个子节点,调用 CM_Request_Device_Eject.
  1. Invoke CM_Get_Child(DevInst) and then CM_Get_Sibling repeatedly to go over all child nodes of the hub (i.e. the USB devices).
  2. For each child node, call CM_Request_Device_Eject.

这篇关于如何使用 Windows API 重置 USB 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆