Search code examples
vb.netgetusbpid

How to get VID and PID in VB.NET


I need to create a program that detect if a certain USB device is plugged. So lets say that we have a device that have VID (Vendor ID) = 9839 and PID (Product ID) = 5453.

I need a code that when i plug the device, the program get automatically the VID and the PID of the device, and write them in two texboxes.

After that is simple, I use:

If textbox1.Text = "9839" And textbox2.Text = "5453" then
   MsgBox("You plugged the device!")
Else
   MsgBox("Device is not plugged")
End If

But i need the code for getting VID and PID of the inserted device into textboxes. So if someone could help me, let me know :)

I tried a solution using USBCLASSLibrary Demo wich is a free dll but my pc is a x64 one and the dll is x32 so i get a error in C# (Bad Image Format) or something else.

I tried using a code found on CodeProject

        private void USBPort_USBDeviceAttached(object sender, 
             USBClass.USBDeviceEventArgs e)
{
   if (!MyUSBDeviceConnected)
   {
      if (USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID, 
                                ref USBDeviceProperties, false))
      {
         //My Device is connected
         MyUSBDeviceConnected = true;
          }
       }
    }

private void USBPort_USBDeviceRemoved(object sender, 
             USBClass.USBDeviceEventArgs e)
{
   if (!USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID, 
                              ref USBDeviceProperties, false))
   {
      //My Device is removed
      MyUSBDeviceConnected = false;``
   }
}

Solution

  • Have you tried HIDs?

    Debug.WriteLine("  HIDD_ATTRIBUTES structure filled without error.")
                                Debug.WriteLine("  Structure size: " & MyHid.DeviceAttributes.Size)
                                Debug.WriteLine("  Vendor ID: " & Hex(MyHid.DeviceAttributes.VendorID))
                                Debug.WriteLine("  Product ID: " & Hex(MyHid.DeviceAttributes.ProductID))
                                Debug.WriteLine("  Version Number: " & Hex(MyHid.DeviceAttributes.VersionNumber))
    

    And then, try :

     Try
            myVendorID = Int32.Parse(txtVendorID.Text, NumberStyles.AllowHexSpecifier)
            myProductID = Int32.Parse(txtProductID.Text, NumberStyles.AllowHexSpecifier)
    
        Catch ex As Exception
    
        End Try