How to get PID,VID from an USB device

ArjunMK

Member
Joined
Apr 17, 2008
Messages
5
Programming Experience
1-3
Dear all

I have got an acedamic project where, in the part 1 of the project, I need to read an usb device and get its PID, VID and other info. I need to do it in VB.Net.

Is there any procedure while working from Visual studio SDK, after surfing thru net for sometime I have learnt that there is one 'HID Class' which is supported only in Microsoft DDK .
Please help me regarding this matter.
 
Still can't do the thing !!! :(

Please tell me the name-spaces I need to import, for example to use 'SWbemObjectSet ' what I need to import. Please tell me since I am new to this sort i/o programming.


P.S: I am using Dot net framework 1 and 1.1.
 
You will have to add a reference to the WMI Scripting type library in your project library.

1. Select 'Add References' from the Project menu.
2. In the Available References box, select Microsoft WBEM Scripting V1.X Library or Microsoft WMI Scripting V1.X Library (whichever appears).
3. If no suitable option appears in the References list, add it by using Browse in the References box. The Browse opens an Add Reference box that enables you to locate the WbemScripting type library.
The WbemScripting type library resides in the file Wbemdisp.tlb in the %windir%\System32\Wbem directory.

C:\WINDOWS\system32\wbem\Wbemdisp.tlb

You can then use WbemScripting.SWbemObjectSet and WbemScripting.SWbemObject
 
Why use this COM library when .Net has System.Management for WMI?
I had a look through the WMI classes, but didn't find Product/Vendor ID from there though.
 
Thanks for pointing that out John.

System.Managemt class will be a much better option rather than using the Wbemdisp.tlb type library. Following code will get the USB DeviceID, using System.Managemet class (add reference to System.Management.dll):

VB.NET:
Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
Dim USB As System.Management.ManagementObject

For Each USB In USBCollection
       Me.ListBox2.Items.Add("Description = " & USB("Name").ToString())
       Me.ListBox2.Items.Add("Device ID = " & USB("deviceid").ToString())
       Me.ListBox2.Items.Add("PNP Device ID = " & USB("PNPDeviceID").ToString())
Next USB
 
Vishal Rana, does PNPDeviceID of USBHub give you PID/VID of USB devices as OP asked for?
 
According to Scripting Guy!...
There isn’t a Dynamic class devoted to USB devices: you can’t return a collection of USB devices the same way you can use Win32_Services to return a collection of all the services on a computer. Instead, you have to use a WMI Association class (Win32_USBControllerDevice) that associates a USB controller with a USB device. With that information you can identify the dependent entity in the association (which happens to be the device associated with a USB controller) and can then query the Win32_PNPEntity to get information about the device itself. (Unfortunately, Win32_USBControllerDevice returns nothing about the device except the somewhat-cryptic device ID.)......


So here's my code (using System.Management class) this will list the USB Device (even the ones connected to your computer) name and DeviceID:

VB.NET:
        Dim strDeviceName As String
        Dim strQuotes As String
        Dim arrDeviceNames As Array
        Dim USBDevice As System.Management.ManagementObject
        Dim objReturnCollection As System.Management.ManagementObjectCollection

        'Dim ObjScope As New System.Management.ManagementScope("\\FullNameOfYourComputer\root\cimv2")    'This is optional. Can be used for remote connections.

        Dim SearcherUSBDevicesCollection As New System.Management.ManagementObjectSearcher("Select * from Win32_USBControllerDevice")
        Dim ReturnUSBDevicesCollection As System.Management.ManagementObjectCollection
        ReturnUSBDevicesCollection = SearcherUSBDevicesCollection.Get

        'Or
        'Dim USBDevicesClass As New System.Management.ManagementClass("Win32_USBControllerDevice")
        'Dim ReturnUSBDevicesCollection As System.Management.ManagementObjectCollection = USBDevicesClass.GetInstances()


        For Each USBDevice In ReturnUSBDevicesCollection
            strDeviceName = USBDevice.Properties("Dependent").Value.ToString()
            strQuotes = Chr(34)
            strDeviceName = Replace(strDeviceName, strQuotes, "")
            arrDeviceNames = Split(strDeviceName, "=")
            strDeviceName = arrDeviceNames(1)

            Dim objSearcher As New System.Management.ManagementObjectSearcher("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
            objReturnCollection = objSearcher.Get()

            Dim objReturn As System.Management.ManagementObject

            For Each objReturn In objReturnCollection
                Me.ListBox4.Items.Add("Description: " & objReturn("Name").ToString())
                Me.ListBox4.Items.Add("DeviceID: " & objReturn("DeviceID").ToString())

            Next
        Next


Follwoing links should clarify the code further:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar05/hey0315.mspx

http://www.csharphelp.com/archives2/archive334.html

http://msdn.microsoft.com/en-us/library/system.management.managementscope.aspx
 
Last edited:
Truly excellent, so what you're saying is that one can parse the VID/PID from the DeviceID string.
 
Thanks Mr Rana.
Its working very well !!

Now pls look at your following code :
" Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
Dim USB As System.Management.ManagementObject

For Each USB In USBCollection
Me.ListBox2.Items.Add("Description = " & USB("Name").ToString())
Me.ListBox2.Items.Add("Device ID = " & USB("deviceid").ToString())
Me.ListBox2.Items.Add("PNP Device ID = " & USB("PNPDeviceID").ToString())
Next USB "

Is there any attribute other than "Name" and "DeviceID" ? I mean can I get any other information thru this 'usb' Management Object.
 
Back
Top