Jud*_*den 10 c# windows usb wmi wmi-query
我有一个USB设备,在命令时使用不同的接口,VID,PID和序列号进行枚举,并且我想在发生此更改后跟踪物理设备.我的想法是通过它的枢纽和港口位置来跟踪它.
该Win32_PnPSignedDriver类有一个"位置"栏,似乎是完美的(例如Port_#0001.Hub_#0010),但它仅包含设备的位置是第一次加载驱动程序时.将硬件插入不同的端口不会更新该字段.
但是,该信息在某处可用,因为在通过"设备管理器"查看设备时,"详细信息"选项卡下有"位置信息"字段.可以通过WMI查询或其他方法检索此信息吗?有没有更好的方法来解决这个问题?
编辑:我知道这听起来像一个奇怪的场景.这些设备中的微控制器包含一个枚举为CDC设备(即串行端口)并允许编程的ROM.在制造过程中,跟踪设备,因为它在制造商的ROM(唯一的VID/PID /序列号)和我的自定义固件接口(不同的VID/PID /序列号)之间变化是有益的.
小智 8
我知道自从这个答案的任何活动以来已经有一段时间了,但是我正在开发一个需要类似功能的项目,我可以告诉你它确实是可能的.据我所知,它确实需要DDK和PInvoke,这个信息没有C#或WMI接口.它需要打开低级USB根集线器设备并直接向它们发送驱动程序IOCTL命令.
好消息是,Microsoft提供了一个示例C++应用程序,它完全枚举所有USB设备并准确显示它们所连接的端口.该应用程序是USBView示例应用程序.
我想你会发现如果你编译并运行这个应用程序,你会发现它确切地显示了你的设备插入的位置,如果你将任何设备插入该端口,它会显示在同一个地方.如果你创建一个非托管C++ DLL可能会更容易,它提供了一些C#应用程序可以用来获取所需信息的调用.
它有关于它的代码中的"EnumerateHubPorts()"函数的说法:
给定开放式集线器的句柄和集线器上的下游端口数量,向集线器发送集线器的每个下游端口的IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX请求,以获取有关连接到每个端口的设备(如果有)的信息.
要了解这一切需要的东西(一切都必须从顶部开始枚举,即使你只对一个端口感兴趣),这里是代码中enum.c文件顶部列出的注释:
/*
This source file contains the routines which enumerate the USB bus
and populate the TreeView control.
The enumeration process goes like this:
(1) Enumerate Host Controllers and Root Hubs
EnumerateHostControllers()
EnumerateHostController()
Host controllers currently have symbolic link names of the form HCDx,
where x starts at 0. Use CreateFile() to open each host controller
symbolic link. Create a node in the TreeView to represent each host
controller.
GetRootHubName()
After a host controller has been opened, send the host controller an
IOCTL_USB_GET_ROOT_HUB_NAME request to get the symbolic link name of
the root hub that is part of the host controller.
(2) Enumerate Hubs (Root Hubs and External Hubs)
EnumerateHub()
Given the name of a hub, use CreateFile() to map the hub. Send the
hub an IOCTL_USB_GET_NODE_INFORMATION request to get info about the
hub, such as the number of downstream ports. Create a node in the
TreeView to represent each hub.
(3) Enumerate Downstream Ports
EnumerateHubPorts()
Given an handle to an open hub and the number of downstream ports on
the hub, send the hub an IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX
request for each downstream port of the hub to get info about the
device (if any) attached to each port. If there is a device attached
to a port, send the hub an IOCTL_USB_GET_NODE_CONNECTION_NAME request
to get the symbolic link name of the hub attached to the downstream
port. If there is a hub attached to the downstream port, recurse to
step (2).
GetAllStringDescriptors()
GetConfigDescriptor()
Create a node in the TreeView to represent each hub port
and attached device.
*/
Run Code Online (Sandbox Code Playgroud)