【问题标题】:How to get VID and PID in VB.NET如何在 VB.NET 中获取 VID 和 PID
【发布时间】:2013-12-29 12:49:45
【问题描述】:

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

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

那很简单,我用:

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

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

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

【问题讨论】:

  • “要求代码的问题必须证明对正在解决的问题有最低限度的了解。包括尝试的解决方案、它们为什么不起作用以及预期的结果。”

标签: vb.net get usb pid


【解决方案1】:

您尝试过 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

【讨论】:

    【解决方案2】:

    创建名为 cmbHdd 的组合框并将代码放在表单上。这将使用 USB 设备填充组合,并从 Win32_DiskDrive 以及 Win32_USBHub 获取 PID 和供应商 ID 的所有必需信息 希望这可以帮助.. 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多