In my application, I need to detect whether a USB device is physically
connected to the local machine, or if it is connected to a remote machine and
made available locally by way of software such as USB Over Network.
I thought maybe I could use WMI and loop through the connected USB devices,
search for the name of the device I'm checking, and then inspect some
property of that device to try to detect whether it's physically located on
my computer.
I don't know enough about WMI to know how to accomplish this, or if it can
even be accomplished. Here's what I've found so far:
Using C#,
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT *
FROM Win32_USBController");
foreach (ManagementObject wmi_USBDevice in searcher.Get())
{
// Check here
}
When I have the device connected locally, all of the objects returned by the
above query have a name similar to "Intel(R) ICH8 Family USB2 Enhanced Host
Controller - 283A". When I have the device connected to a remote machine and
made available to me using USB Over Network, one of the devices returns a
name that is easily identifiable to me as the name of the device I'm looking
for.
What I'm wondering is, is this sufficient? If I find the device name, do I
know it's actually located on a remote machine? It doesn't "feel" right.
If anyone has done something like this and has any suggestions, it would
help me out a lot. Thanks in advance.
-Eric
Why? What's the problem you are addressing?
The issue here is that you have a chicken-and-egg problem. The people
building remote USB systems are doing their damnedest to make it impossible
for you to tell the difference. There is no standard for that kind of
thing, so whatever scheme you come up with might work for one device but
not for another.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
I’ve never done this myself – and I’m not going to buy USB Over Network and
install it on my computers just to answer your question. Nevertheless I
think I can point you in the right direction. Keep in mind that this is an
‘advanced’ subject and it’s hard to tell from your post how much programming
experience you have or how much you already know about this stuff.
On Windows every device is represented by an opaque handle called a DEVNODE
(it’s also called a DEVINST – the terms are interchangeable). Devices are
arranged in a device stack and the stacks form a device tree. Look at this
sample PnP device tree diagram.
http://msdn.microsoft.com/en-us/library/aa489660.aspx
In that diagram the Joystick in the upper left corner is called a ‘child’ of
the USB Hub. And the USB Hub is a ‘child’ of the USB Controller. You can
find the ‘parent’ of a DEVNODE by calling CM_Get_Parent() or
CM_Get_Parent_Ex().
http://msdn.microsoft.com/en-us/library/ms791198.aspx
http://msdn.microsoft.com/en-us/library/ms790882.aspx
The solution to your problem is to find the DEVNODE for the USB device in
question, and then climb down the device tree until you find a DEVNODE that
is connected to physical hardware. If you can’t find actual physical
electronic hardware then you know your ‘USB device’ is a phony.
Specifically, during the process of climbing down the tree you should
eventually transverse the PCI PnP bus driver. Check out these diagrams:
http://msdn.microsoft.com/en-us/library/aa490241.aspx
http://msdn.microsoft.com/en-us/library/aa490242.aspx
There is a utility in the WDK (Windows Driver Kit) called USBView.exe. It’s
a GUI application that allows you to browse all USB controllers and connected
USB devices on your system. It comes with source code written in C, but
Emmet Gray has ported it to C#.
http://www.emmet-gray.com/Articles/USB_SerialNumbers.htm
http://www.emmet-gray.com/Programs/USBView.zip
Note that most folks use that code to find the serial numbers for USB
devices – but it will also serve as a solid foundation to solve the problem
you are trying to solve.
Also, you might find better answers to these questions over at the USB
Developer’s Forum.
https://www.usb.org/phpbb/viewforum.php?f=1&sid=b8918ad64179d43160bbca17d0091c82
I hope that made sense and I hope that helped.