Solved

VB.Net Usb

Posted on 2010-09-04
9
837 Views
Last Modified: 2012-05-10
How would I create a loop that waits for a remmovable usb device (like a flash drive) to be plugged in, then when it is, show a messagebox?
0
Comment
Question by:XGenwareS
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33603831

There is a couple of different ways you might get this information.

Using RegisterDeviceNotification() specify the global identifier for USB devices {A5DCBF10-6530-11D2-901F-00C04FB951ED}.

You won't need a loop because windows will broadcast the message to the WindowProc() so using this method would only require that you override WndProc() on your Form after registering the message.


 
0
 

Author Comment

by:XGenwareS
ID: 33603966
Could I please see an example of this?
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33603973
Sure.. But I have to write an example so it will take few minutes...
0
 

Author Comment

by:XGenwareS
ID: 33604013
ya sure take your time. thanks alot!
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33604192
Add the following example to the form (copy paste friendly)  =)

Imports System.Runtime.InteropServices

Imports Microsoft.Win32.SafeHandles

'egl1044

Public Class Form1

    ''' <summary>

    ''' Detect USB notifications.

    ''' </summary>

    ''' <param name="m"></param>

    ''' <remarks></remarks>

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Dim lpdb As UsbNotification.DevBroadcastHdr

        Dim lpdbv As UsbNotification.DevBroadcastVolume

        Dim lpdbdi As UsbNotification.DevBroadcastDeviceInterface

        Static fUsb As Boolean

        If m.Msg = UsbNotification.WM_DEVICECHANGE Then

            Select Case m.WParam.ToInt32

                Case UsbNotification.DBT_DEVICEARRIVAL

                    lpdb = CType(m.GetLParam(lpdb.GetType), UsbNotification.DevBroadcastHdr)

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_DEVICEINTERFACE Then

                        lpdbdi = CType(m.GetLParam(lpdbdi.GetType), UsbNotification.DevBroadcastDeviceInterface)

                        fUsb = True

                    End If

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_VOLUME AndAlso fUsb Then

                        lpdbv = CType(m.GetLParam(lpdbv.GetType), UsbNotification.DevBroadcastVolume)

                        fUsb = False

                        ' Inserted

                        Debug.Print("Inserted @ drive= {0}", UsbNotification.GetDrive(lpdbv.DBCV_UnitMask))

                    End If

                Case UsbNotification.DBT_DEVICEREMOVECOMPLETE

                    lpdb = CType(m.GetLParam(lpdb.GetType), UsbNotification.DevBroadcastHdr)

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_DEVICEINTERFACE AndAlso fUsb Then

                        lpdbdi = CType(m.GetLParam(lpdbdi.GetType), UsbNotification.DevBroadcastDeviceInterface)

                        fUsb = True

                    End If

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_VOLUME Then

                        lpdbv = CType(m.GetLParam(lpdbv.GetType), UsbNotification.DevBroadcastVolume)

                        fUsb = False

                        ' Removed

                        Debug.Print("Removed @ drive= {0}", UsbNotification.GetDrive(lpdbv.DBCV_UnitMask))

                    End If

            End Select

        End If

        MyBase.WndProc(m)

    End Sub



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

        UsbNotification.Start(Me.Handle)

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        UsbNotification.Cancel()

    End Sub

End Class



''' <summary>

''' Registers for USB notifications.

''' </summary>

''' <remarks></remarks>

Public Class UsbNotification

    Public Const WM_DEVICECHANGE As Integer = &H219

    Public Const DBT_DEVICEARRIVAL As Integer = 32768

    Public Const DBT_DEVICEREMOVECOMPLETE As Integer = 32772

    Public Const DBT_DEVTYP_DEVICEINTERFACE As Integer = 5

    Public Const DBT_DEVTYP_VOLUME As Integer = &H2



    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastHdr

        Dim dbch_size As UInt32

        Dim dbch_devicetype As UInt32

        Dim dbch_reserved As UInt32

    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastDeviceInterface

        Dim dbcc_size As UInt32

        Dim dbcc_devicetype As UInt32

        Dim dbcc_reserved As UInt32

        Dim dbcc_classguid As Guid

        Dim dbcc_name As IntPtr

    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastVolume

        Dim DBCV_Size As UInt32

        Dim DBCV_DeviceType As UInt32

        Dim DBCV_Reserved As UInt32

        Dim DBCV_UnitMask As UInt32

        Dim DBCV_Flags As UInt16

    End Structure



    <DllImport("user32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function RegisterDeviceNotification(ByVal hRecipient As IntPtr, _

                                                       ByRef NotificationFilter As DevBroadcastDeviceInterface, _

                                                       ByVal Flags As UInt32) As SafeDeviceNotificationHandle

    End Function



    Private Shared hDeviceNotify As SafeDeviceNotificationHandle = Nothing

    Private Sub New()

    End Sub

    Public Shared Sub Start(ByVal hRecipient As IntPtr)

        Dim dbd As DevBroadcastDeviceInterface = Nothing

        If hDeviceNotify IsNot Nothing Then

            UsbNotification.Cancel()

        End If

        dbd.dbcc_size = CUInt(Marshal.SizeOf(dbd))

        dbd.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE

        dbd.dbcc_classguid = New Guid("{A5DCBF10-6530-11D2-901F-00C04FB951ED}")

        hDeviceNotify = RegisterDeviceNotification(hRecipient, dbd, 0)

        If hDeviceNotify.IsInvalid Then

            Debug.Print("Failed")

        Else

            Debug.Print("Register OK")

        End If

    End Sub

    Public Shared Sub Cancel()

        If hDeviceNotify IsNot Nothing Then

            hDeviceNotify.Close()

            hDeviceNotify.Dispose()

            hDeviceNotify = Nothing

            Debug.Print("UnRegister OK")

        End If

    End Sub

    Public Shared Function GetDrive(ByVal UnitMask As UInteger) As String

        GetDrive = Chr(CInt(65 + (Math.Log(UnitMask) / Math.Log(2))))

    End Function

End Class



''' <summary>

''' Represents a wrapper class for a device notification handle.

''' </summary>

''' <remarks></remarks>

Public Class SafeDeviceNotificationHandle

    Inherits SafeHandleZeroOrMinusOneIsInvalid

    <DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function UnregisterDeviceNotification(ByVal Handle As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

    End Function

    Private Sub New()

        MyBase.New(True)

    End Sub

    Protected Overloads Overrides Function ReleaseHandle() As Boolean

        Return UnregisterDeviceNotification(handle)

    End Function

End Class

Open in new window

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 33604266
Whoops.. I made a minor mistake in the WndProc() in the above example @ line 31 the bit flag checksum should really be placed @ line 35. If you don't fix this then CDROM devices will also be detected on Removal.
Use the updated example below which is the fixed version of the above lines.

Imports System.Runtime.InteropServices

Imports Microsoft.Win32.SafeHandles

'egl1044

Public Class Form1

    ''' <summary>

    ''' Detect USB notifications.

    ''' </summary>

    ''' <param name="m"></param>

    ''' <remarks></remarks>

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Dim lpdb As UsbNotification.DevBroadcastHdr

        Dim lpdbv As UsbNotification.DevBroadcastVolume

        Dim lpdbdi As UsbNotification.DevBroadcastDeviceInterface

        Static fUsb As Boolean

        If m.Msg = UsbNotification.WM_DEVICECHANGE Then

            Select Case m.WParam.ToInt32

                Case UsbNotification.DBT_DEVICEARRIVAL

                    lpdb = CType(m.GetLParam(lpdb.GetType), UsbNotification.DevBroadcastHdr)

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_DEVICEINTERFACE Then

                        lpdbdi = CType(m.GetLParam(lpdbdi.GetType), UsbNotification.DevBroadcastDeviceInterface)

                        fUsb = True

                    End If

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_VOLUME AndAlso fUsb Then

                        lpdbv = CType(m.GetLParam(lpdbv.GetType), UsbNotification.DevBroadcastVolume)

                        fUsb = False

                        ' Inserted

                        Debug.Print("Inserted @ drive= {0}", UsbNotification.GetDrive(lpdbv.DBCV_UnitMask))

                    End If

                Case UsbNotification.DBT_DEVICEREMOVECOMPLETE

                    lpdb = CType(m.GetLParam(lpdb.GetType), UsbNotification.DevBroadcastHdr)

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_DEVICEINTERFACE Then

                        lpdbdi = CType(m.GetLParam(lpdbdi.GetType), UsbNotification.DevBroadcastDeviceInterface)

                        fUsb = True

                    End If

                    If lpdb.dbch_devicetype = UsbNotification.DBT_DEVTYP_VOLUME AndAlso fUsb Then

                        lpdbv = CType(m.GetLParam(lpdbv.GetType), UsbNotification.DevBroadcastVolume)

                        fUsb = False

                        ' Removed

                        Debug.Print("Removed @ drive= {0}", UsbNotification.GetDrive(lpdbv.DBCV_UnitMask))

                    End If

            End Select

        End If

        MyBase.WndProc(m)

    End Sub



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

        UsbNotification.Start(Me.Handle)

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        UsbNotification.Cancel()

    End Sub

End Class



''' <summary>

''' Registers for USB notifications.

''' </summary>

''' <remarks></remarks>

Public Class UsbNotification

    Public Const WM_DEVICECHANGE As Integer = &H219

    Public Const DBT_DEVICEARRIVAL As Integer = 32768

    Public Const DBT_DEVICEREMOVECOMPLETE As Integer = 32772

    Public Const DBT_DEVTYP_DEVICEINTERFACE As Integer = 5

    Public Const DBT_DEVTYP_VOLUME As Integer = &H2



    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastHdr

        Dim dbch_size As UInt32

        Dim dbch_devicetype As UInt32

        Dim dbch_reserved As UInt32

    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastDeviceInterface

        Dim dbcc_size As UInt32

        Dim dbcc_devicetype As UInt32

        Dim dbcc_reserved As UInt32

        Dim dbcc_classguid As Guid

        Dim dbcc_name As IntPtr

    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure DevBroadcastVolume

        Dim DBCV_Size As UInt32

        Dim DBCV_DeviceType As UInt32

        Dim DBCV_Reserved As UInt32

        Dim DBCV_UnitMask As UInt32

        Dim DBCV_Flags As UInt16

    End Structure



    <DllImport("user32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function RegisterDeviceNotification(ByVal hRecipient As IntPtr, _

                                                       ByRef NotificationFilter As DevBroadcastDeviceInterface, _

                                                       ByVal Flags As UInt32) As SafeDeviceNotificationHandle

    End Function



    Private Shared hDeviceNotify As SafeDeviceNotificationHandle = Nothing

    Private Sub New()

    End Sub

    Public Shared Sub Start(ByVal hRecipient As IntPtr)

        Dim dbd As DevBroadcastDeviceInterface = Nothing

        If hDeviceNotify IsNot Nothing Then

            UsbNotification.Cancel()

        End If

        dbd.dbcc_size = CUInt(Marshal.SizeOf(dbd))

        dbd.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE

        dbd.dbcc_classguid = New Guid("{A5DCBF10-6530-11D2-901F-00C04FB951ED}")

        hDeviceNotify = RegisterDeviceNotification(hRecipient, dbd, 0)

        If hDeviceNotify.IsInvalid Then

            Debug.Print("Failed")

        Else

            Debug.Print("Register OK")

        End If

    End Sub

    Public Shared Sub Cancel()

        If hDeviceNotify IsNot Nothing Then

            hDeviceNotify.Close()

            hDeviceNotify.Dispose()

            hDeviceNotify = Nothing

            Debug.Print("UnRegister OK")

        End If

    End Sub

    Public Shared Function GetDrive(ByVal UnitMask As UInteger) As String

        Return Chr(CInt(65 + (Math.Log(UnitMask) / Math.Log(2))))

    End Function

End Class



''' <summary>

''' Represents a wrapper class for a device notification handle.

''' </summary>

''' <remarks></remarks>

Public Class SafeDeviceNotificationHandle

    Inherits SafeHandleZeroOrMinusOneIsInvalid

    <DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)> _

    Private Shared Function UnregisterDeviceNotification(ByVal Handle As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

    End Function

    Private Sub New()

        MyBase.New(True)

    End Sub

    Protected Overloads Overrides Function ReleaseHandle() As Boolean

        Return UnregisterDeviceNotification(handle)

    End Function

End Class

Open in new window

0
 

Author Comment

by:XGenwareS
ID: 33604296
wow thx alot. ill try it out as soon as I get home
0
 

Author Comment

by:XGenwareS
ID: 33604601
I'm getting this error when I insert or remove a usb drive:

System.InvalidCastException: Conversion from string "Q" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format.

Visual Studio isn't giving me the exact line the error is on. It just stops working. I had to run it without the debugger to get the error message
0
 

Author Comment

by:XGenwareS
ID: 33604646
Nvm I found the problem. It was at this line:

Debug.Print("Inserted @ drive= {0}", UsbNotification.GetDrive(lpdbv.DBCV_UnitMask))

I just changed it to:

 MsgBox("Inserted")

That seemed to fix it. It works perfect now!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now