如何在VB.NET中获取VID和PID

时间:2013-12-29 12:49:46

标签: vb.net get usb pid

我需要创建一个程序来检测某个USB设备是否已插入。我们假设我们的设备有VID (Vendor ID) = 9839PID (Product ID) = 5453

我需要一个代码,当我插入设备时,程序会自动获取设备的VIDPID,并将它们写入两个texbox中。

之后很简单,我使用:

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

但我需要将插入设备的VIDPID放入文本框的代码。 所以,如果有人可以帮助我,请告诉我:)。

我尝试使用USBCLASSLibrary Demo解决方案 这是一个免费的DLL,但我的电脑是一个x64,而dll是x32,所以我得到一个错误的C#(坏图像格式)或其他。

我尝试使用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;``
   }
}

2 个答案:

答案 0 :(得分:4)

你试过HID吗?

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))

然后,尝试:

 Try
        myVendorID = Int32.Parse(txtVendorID.Text, NumberStyles.AllowHexSpecifier)
        myProductID = Int32.Parse(txtProductID.Text, NumberStyles.AllowHexSpecifier)

    Catch ex As Exception

    End Try

答案 1 :(得分:-2)

创建名为cmbHdd的组合框并将代码放在表单上。这将填充usb设备的组合,并从Win32_DiskDrive获取所有必需信息以及获取PID和供应商ID的Win32_USBHub 希望这可以帮助.. malvastyle团队

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim mosDisks As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")

    For Each moDisk As ManagementObject In mosDisks.[Get]()
        cmbHdd.Items.Add(moDisk("Model").ToString())
    Next
End Sub

Private Sub cmbHdd_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbHdd.SelectedIndexChanged
    Try
        Dim GetQuery As String = ("SELECT * FROM Win32_DiskDrive WHERE Model = '" & cmbHdd.SelectedItem & "'")
        Dim mosDisks As New ManagementObjectSearcher(GetQuery)

        For Each moDisk As ManagementObject In mosDisks.[Get]()
            lblType.Text = moDisk("MediaType").ToString().Trim
            lblModel.Text = moDisk("Model").ToString().Trim
            lblSerial.Text = moDisk("SerialNumber").ToString().Trim
            lblInterface.Text = moDisk("InterfaceType").ToString().Trim
            lblCapacity.Text = moDisk("Size").ToString() & " bytes (" & Math.Round((((CDbl(Convert.ToDouble(moDisk("Size"))) / 1024) / 1024) / 1024), 2) & " GB)".Trim
            lblPartitions.Text = moDisk("Partitions").ToString().Trim
            lblSignature.Text = moDisk("Signature").ToString().Trim
            lblFirmware.Text = moDisk("FirmwareRevision").ToString().Trim
            lblSylinders.Text = moDisk("TotalCylinders").ToString().Trim
            lblSectors.Text = moDisk("TotalSectors").ToString().Trim
            lblHeads.Text = moDisk("TotalHeads").ToString().Trim
            lblTracks.Text = moDisk("TotalTracks").ToString().Trim
            lblBytesPerSector.Text = moDisk("BytesPerSector").ToString().Trim
            lblSectorsPerTrack.Text = moDisk("SectorsPerTrack").ToString().Trim
            lblTrackPerCylinder.Text = moDisk("TracksPerCylinder").ToString().Trim
            ' lblProductID.Text = moDisk("PNPDeviceID").ToString().Trim
            lblVendorID.Text = moDisk("PNPDeviceID").ToString().Trim
        Next

        Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
        Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
        Dim _USB As System.Management.ManagementObject
        Dim _tempID As String = ""

        For Each _USB In USBCollection
            Dim splitString As String() = (_USB("DeviceID")).Split(New [Char]() {"/"c, "\"c, CChar(vbTab)})
            _tempID = splitString(1)
            If (lblVendorID.Text).Contains(splitString(2)) Then
                lblSerial.Text = splitString(2)
                Exit For
            End If
            _tempID = ""
        Next

        If _tempID <> "" Then
            Dim splitID As String() = _tempID.Split(New [Char]() {"&"c, CChar(vbTab)})
            Dim splitVendor As String() = splitID(0).Split(New [Char]() {"_"c, CChar(vbTab)})
            Dim splitProduct As String() = splitID(1).Split(New [Char]() {"_"c, CChar(vbTab)})

            lblVendorID.Text = splitVendor(1)
            lblProductID.Text = splitProduct(1)
        End If

    Catch ex As Exception
    End Try
End Sub