title: Detect USB device upon plugged in and get its WMI information in VB
body: <p>I've been trying to create a quick app that detects a usb device has been pluged in to get all its information.</p>
  
  <p>At the moment I've got as far as to detect all the usb devices plugged in but whenever I try to get the information of the plugged device I get an error in VB</p>
  
  <p>this is my code.</p>
  
  <pre><code>Imports System.Management
  Imports System.Runtime.InteropServices
  
  Public Class Form1
      'Used to detected if any of the messages are any of these constants values.
      Private Const WM_DEVICECHANGE As Integer = &amp;H219
      Private Const DBT_DEVICEARRIVAL As Integer = &amp;H8000
      Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &amp;H8004
  Private Const DBT_DEVTYP_VOLUME As Integer = &amp;H2  '
  '
  'Get the information about the detected volume.
  Private Structure DEV_BROADCAST_VOLUME
      Dim Dbcv_Size As Integer
      Dim Dbcv_Devicetype As Integer
      Dim Dbcv_Reserved As Integer
      Dim Dbcv_Unitmask As Integer
      Dim Dbcv_Flags As Short
  End Structure
  
  Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
      Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
      Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
      Dim USB As System.Management.ManagementObject
  
      If M.Msg = WM_DEVICECHANGE Then
  
  
          For Each USB In USBCollection
              RTextBox1.Text = RTextBox1.Text &amp; "Description = " &amp; USB("Name").ToString()
              RTextBox1.Text = RTextBox1.Text &amp; "Device ID = " &amp; USB("deviceid").ToString()
              RTextBox1.Text = RTextBox1.Text &amp; "PNP Device ID = " &amp; USB("PNPDeviceID").ToString()
          Next USB
  
      End If
      MyBase.WndProc(M)
  End Sub
  
  
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      ScanUSB()
  
  
  End Sub
  
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      RTextBox1.Text = ""
  End Sub
  
  Private Sub ScanUSB()
      Dim strComputer As String
      Dim objWMIService As Object
      Dim colDevices As Object
      Dim strDeviceName As String
      Dim strQuotes As String
      Dim arrDeviceNames As Array
      Dim colUSBDevices As Object
      Dim objUSBDevice As Object
      Dim item2 As String
  
  
      strComputer = "."
  
      objWMIService = GetObject("winmgmts:\\" &amp; strComputer &amp; "\root\cimv2")
      colDevices = objWMIService.ExecQuery _
          ("Select * From Win32_USBControllerDevice")
  
      For Each objDevice In colDevices
          strDeviceName = objDevice.Dependent
          strQuotes = Chr(34)
          strDeviceName = Replace(strDeviceName, strQuotes, "")
          arrDeviceNames = Split(strDeviceName, "=")
          strDeviceName = arrDeviceNames(1)
          colUSBDevices = objWMIService.ExecQuery _
              ("Select * From Win32_PnPEntity Where DeviceID = '" &amp; strDeviceName &amp; "'")
          For Each objUSBDevice In colUSBDevices
              RTextBox1.Text = RTextBox1.Text &amp; "Availability: " &amp; objUSBDevice.Availability &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Caption: " &amp; objUSBDevice.Caption &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "ClassGuid: " &amp; objUSBDevice.ClassGuid &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "ConfigManagerErrorCode: " &amp; objUSBDevice.ConfigManagerErrorCode &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "ConfigManagerUserConfig: " &amp; objUSBDevice.ConfigManagerUserConfig &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "CreationClassName: " &amp; objUSBDevice.CreationClassName &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Description: " &amp; objUSBDevice.Description &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "DeviceID: " &amp; objUSBDevice.DeviceID &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "ErrorCleared: " &amp; objUSBDevice.ErrorCleared &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "ErrorDescription: " &amp; objUSBDevice.ErrorDescription &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "InstallDate: " &amp; objUSBDevice.InstallDate &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "LastErrorCode: " &amp; objUSBDevice.LastErrorCode &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Manufacturer: " &amp; objUSBDevice.Manufacturer &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Name: " &amp; objUSBDevice.Name &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "PNPDeviceID: " &amp; objUSBDevice.PNPDeviceID &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "PowerManagementCapabilities: " &amp; objUSBDevice.PowerManagementCapabilities &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "PowerManagementSupported: " &amp; objUSBDevice.PowerManagementSupported &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Service: " &amp; objUSBDevice.Service &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "Status: " &amp; objUSBDevice.Status &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "StatusInfo: " &amp; objUSBDevice.StatusInfo &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "SystemCreationClassName: " &amp; objUSBDevice.SystemCreationClassName &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; "SystemName: " &amp; objUSBDevice.SystemName &amp; Chr(13)
              For Each item2 In objUSBDevice.HardwareID
                  RTextBox1.Text = RTextBox1.Text &amp; "HardwareID: " &amp; item2 &amp; Chr(13)
              Next
              RTextBox1.Text = RTextBox1.Text &amp; objUSBDevice.Description() &amp; Chr(13)
              RTextBox1.Text = RTextBox1.Text &amp; Chr(13) &amp; Chr(13)
          Next
      Next
  
  End Sub
  </code></pre>
  
  <p>End Class</p>