Results 1 to 2 of 2

Thread: USB Detecter. Please Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    11

    Question USB Detecter. Please Help

    Hello! I have looked everywhere on google and youtube and other websites but have not seen any coding about how to detect if a USB gets disconnected. And now for my program i need that code, if you are good a visual basic 2010 and can help me please comment with a solution https://gyazo.com/06371278e308ae159b2e78528acffca4 Thanks!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: USB Detecter. Please Help

    Windows raises events to tell you when a USB device is inserted and removed.

    If you search for WM_DEVICECHANGE you should find many example, like,,
    vb.net Code:
    1. Public Class Form1
    2.     Private Const WM_DEVICECHANGE As Integer = &H219
    3.     Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    4.     Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
    5.     Private Const DBT_DEVTYP_VOLUME As Integer = &H2
    6.  
    7.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    8.         'check for the DEVICECHANGE message (Detects USB devices)
    9.         If m.Msg = WM_DEVICECHANGE Then
    10.             Select Case m.WParam.ToInt32
    11.                 ' if a device was added or removed then update usb drives list.
    12.                 Case DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE
    13.                     Dim DevType As Integer = Marshal.ReadInt32(m.LParam, 4)
    14.                     If DevType = DBT_DEVTYP_VOLUME Then
    15.                       ' DO SOMETHING...
    16.                       '  Me.BeginInvoke(New Action(AddressOf GetPenDrives))
    17.                     End If
    18.             End Select
    19.         End If
    20.         MyBase.WndProc(m)
    21.     End Sub
    22.  
    23. End Class

    VB10 Test project, ripped and slapped together from one of my apps.
    Attachment 134285
    EDIT, Updated Zip , includes the ownerdrawn combobox as shown in image above.
    Last edited by Edgemeal; Jan 15th, 2016 at 03:28 PM. Reason: update file again!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width