What's new

Tutorial How to Detect USB Plugin and Unplug using VB6

Status
Not open for further replies.

CodeWarrior

Addict
Joined
Feb 16, 2014
Posts
23
Reaction
16
Points
81
Age
39
This tutorial show you how to monitor USB device if plug or unplug and the drive letter

First download 2 usb image type. This 2 image is use for monitoring if Plugin or unplug.
Download Image Usb Open and Usb Close...

Second open your vb6 and create new project and name it

Third Add the following object to the form

1. One Label
2. One Timer, Set Enable to true and interval to 1
3. Two Image, Image1 change image to USB Close, Image2 change image to USB Open

Now for the code

Code:
Option Compare Text
Dim obj As New StdPicture
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Dim CWDrive(13)
Dim usbDevice As String

Private Sub Form_Load()
   Dim x, i As Byte
     For i = 68 To 80
         x = x + 1
         CWDrive(x) = Chr(i) & ":"
     Next
   Set obj = Image1
End Sub

Private Sub Timer1_Timer()
  For i = 1 To 13
      If GetDriveType(CWDrive(i)) = 2 Then
         Image1.Picture = Image2.Picture
         usbDevice = CWDrive(i)
         Label1.Caption = "USB DETECTED ON DRIVE (" & CWDrive(i) & ")"
      Exit Sub
      Else
         Set Image1.Picture = obj
         usbDevice = ""
      End If
    
     Next
      Label1.Caption = "PLUGIN USB DEVICE"
End Sub

Now Press F5 to start debuging and try to *********** USB Flash Drive
Happy Happy Coding....

This code credited to original creator it's ME... :)
 
Status
Not open for further replies.

Similar threads

Back
Top