How to detect a USB Device (Any Type) ?

DESTROYER

New member
Joined
Oct 20, 2009
Messages
4
Programming Experience
1-3
Hi
I need to detect a usb device when it is connected to PC.
I found next code that is working. But it works only with Mass Storage.
Can anyone help me to detect any type of device ? I am interested in audio devices. Thanks

Imports System.Management

Public Class Form1
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String

Public Sub StartDetection()
' __InstanceOperationEvent will trap both Creation and Deletion of class instances
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")

m_MediaConnectWatcher = New ManagementEventWatcher
m_MediaConnectWatcher.Query = query2
m_MediaConnectWatcher.Start()
End Sub


Private Sub Arrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived

Dim mbo, obj As ManagementBaseObject

' the first thing we have to do is figure out if this is a creation or deletion event
mbo = CType(e.NewEvent, ManagementBaseObject)
' next we need a copy of the instance that was either created or deleted
obj = CType(mbo("TargetInstance"), ManagementBaseObject)

Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(obj("Caption") & " (Drive letter " & GetDriveLetterFromDisk(obj("Name")) & ") has been plugged in")
Else
MsgBox(obj("InterfaceType"))
End If
Case "__InstanceDeletionEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(obj("Caption") & " has been unplugged")
If obj("Caption") = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
Else
MsgBox(obj("InterfaceType"))
End If
Case Else
MsgBox("nope: " & obj("Caption"))
End Select
End Sub

Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""

' WMI queries use the "\" as an escape charcter
Name = Replace(Name, "\", "\\")

' First we map the Win32_DiskDrive instance with the association called
' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion
' instance with the assocation called Win32_LogicalDiskToPartition

oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()

oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk("Name") & ","
Next
Next

Return ans.Trim(","c)
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartDetection()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
m_MediaConnectWatcher.Stop()
Application.Exit()
End Sub
End Class
 
thats because this codes set to look for MassStorage Devices, try altering the code where is needed 'Win32_DiskDrive'" <-- For the MassStorage and other references, just have a quick bash at it :)
 
Hi, i also forgot to mention, if you search this forum for WMI Creator this will more than likely help you, it's a neat little program from Microsoft, theirs a URL on this forum.
 
Hey thanks for the code .

i have coded in new form But need your help as its not showing device attached to my pc.

how it will show the device attached to pc.

please help what to do.
 
Back
Top