View Poll Results: USD Falsh DRive
- Voters
- 0. You may not vote on this poll
-
Drive Size
-
Drive Letter
-
Drive Format
-
Jun 7th, 2008, 03:34 AM
#1
Thread Starter
Hyperactive Member
Detect USB Flsh Drive
Hi all,
While connecting a USB flash drive a command button wants to enable in the VB6 form. If any one have idea please let me know.....
Thanks....
Partha
-
Jun 7th, 2008, 06:29 AM
#2
Re: Detect USB Flsh Drive
-
Jun 8th, 2008, 03:54 PM
#3
Addicted Member
Re: Detect USB Flsh Drive
http://www.experts-exchange.com/Prog..._21325034.html
This will tell you when a device has been removed (and safe to be removed) and will also tell you the name of a new device on arrival.
Place a listbox (Listbox1) on a form (Form1) and add this code:
Code:
'In a form
Option Explicit
Dim hDevNotify As Long
Private Sub Form_Load()
Call DoRegisterDeviceInterface(Me.hWnd, hDevNotify)
glngPrevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf MyWindowProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hDevNotify <> 0 Then
Call UnregisterDeviceNotification(hDevNotify)
End If
'pass control back to previous windows
SetWindowLong Me.hWnd, GWL_WNDPROC, glngPrevWndProc
End Sub
Add a module with this code:
Code:
'In a module
Option Explicit
Public Type GUID
Data1(1 To 4) As Byte
Data2(1 To 2) As Byte
Data3(1 To 2) As Byte
Data4(1 To 8) As Byte
End Type
Public Type DEV_BROADCAST_DEVICEINTERFACE
dbcc_size As Long
dbcc_devicetype As Long
dbcc_reserved As Long
dbcc_classguid As GUID
dbcc_name As Long
End Type
Public Type DEV_BROADCAST_DEVICEINTERFACE2
dbcc_size As Long
dbcc_devicetype As Long
dbcc_reserved As Long
dbcc_classguid As GUID
dbcc_name As String * 1024
End Type
Public Declare Function RegisterDeviceNotification Lib "user32.dll" _
Alias "RegisterDeviceNotificationA" ( _
ByVal hRecipient As Long, _
NotificationFilter As Any, _
ByVal Flags As Long) As Long
Public Declare Function UnregisterDeviceNotification Lib "user32.dll" _
(ByVal hRecipient As Long) As Long
Public Const DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = &H4
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Public Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
Public Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_DEVICECHANGE = &H219
Public Const UNSAFE_REMOVE = &H1C
Public glngPrevWndProc As Long
Public Type DEV_BROADCAST_HDR
dbch_size As Long
dbch_devicetype As Long
dbch_reserved As Long
End Type
Public Const DBT_CONFIGCHANGECANCELED As Long = 25
Public Const DBT_CONFIGCHANGED As Long = 24
Public Const DBT_CUSTOMEVENT As Long = 32744
Public Const DBT_DEVICEARRIVAL As Long = 32768
Public Const DBT_DEVICEQUERYREMOVE As Long = 32769
Public Const DBT_DEVICEQUERYREMOVEFAILED As Long = 32770
Public Const DBT_DEVICEREMOVECOMPLETE As Long = 32772
Public Const DBT_DEVICEREMOVEPENDING As Long = 32771
Public Const DBT_DEVICETYPESPECIFIC As Long = 32773
Public Const DBT_DEVNODES_CHANGED As Long = 7
Public Const DBT_QUERYCHANGECONFIG As Long = 23
Public Const DBT_USERDEFINED As Long = 65535
Public Const DBT_DEVTYP_OEM As Long = 0
Public Const DBT_DEVTYP_DEVNODE As Long = 1
Public Const DBT_DEVTYP_VOLUME As Long = 2
Public Const DBT_DEVTYP_PORT As Long = 3
Public Const DBT_DEVTYP_NET As Long = 4
Public Const DBT_DEVTYP_DEVICEINTERFACE As Long = 5
Public Const DBT_DEVTYP_HANDLE As Long = 6
Public Function MyWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim dbHdr As DEV_BROADCAST_HDR
Dim dbDdb As DEV_BROADCAST_DEVICEINTERFACE2
Select Case Msg
Case WM_DEVICECHANGE
Form1.List1.AddItem "WM_DEVICECHANGE " & Msg
Select Case wParam
Case DBT_CONFIGCHANGECANCELED
Form1.List1.AddItem "wParam = DBT_CONFIGCHANGECANCELED"
Case DBT_CONFIGCHANGED
Form1.List1.AddItem "wParam = DBT_CONFIGCHANGED"
Case DBT_CUSTOMEVENT
Form1.List1.AddItem "wParam = DBT_CUSTOMEVENT"
Case DBT_DEVICEARRIVAL
Form1.List1.AddItem "wParam = DBT_DEVICEARRIVAL"
CopyMemory dbHdr, ByVal (lParam), Len(dbHdr)
Select Case dbHdr.dbch_devicetype
Case DBT_DEVTYP_OEM
Form1.List1.AddItem "Device Type: DBT_DEVTYP_OEM"
Case DBT_DEVTYP_DEVNODE
Form1.List1.AddItem "Device Type: DBT_DEVTYP_DEVNODE"
Case DBT_DEVTYP_VOLUME
Form1.List1.AddItem "Device Type: DBT_DEVTYP_VOLUME"
Case DBT_DEVTYP_PORT
Form1.List1.AddItem "Device Type: DBT_DEVTYP_PORT"
Case DBT_DEVTYP_NET
Form1.List1.AddItem "Device Type: DBT_DEVTYP_NET"
Case DBT_DEVTYP_DEVICEINTERFACE
Form1.List1.AddItem "Device Type: DBT_DEVTYP_DEVICEINTERFACE"
CopyMemory dbDdb, ByVal (lParam), ByVal (dbHdr.dbch_size)
Form1.List1.AddItem "Device Name: " & Mid(dbDdb.dbcc_name, 1, InStr(dbDdb.dbcc_name, Chr(0)))
Case DBT_DEVTYP_HANDLE
Form1.List1.AddItem "Device Type: DBT_DEVTYP_HANDLE"
Case Else
Form1.List1.AddItem "Device Type unknown: " & CStr(dbHdr.dbch_devicetype)
End Select
Case DBT_DEVICEQUERYREMOVE
Form1.List1.AddItem "wParam = DBT_DEVICEQUERYREMOVE"
Case DBT_DEVICEQUERYREMOVEFAILED
Form1.List1.AddItem "wParam = DBT_DEVICEQUERYREMOVEFAILED"
Case DBT_DEVICEREMOVECOMPLETE
Form1.List1.AddItem "wParam = DBT_DEVICEREMOVECOMPLETE"
Case DBT_DEVICEREMOVEPENDING
Form1.List1.AddItem "wParam = DBT_DEVICEREMOVEPENDING"
Case DBT_DEVICETYPESPECIFIC
Form1.List1.AddItem "wParam = DBT_DEVICETYPESPECIFIC"
Case DBT_DEVNODES_CHANGED
Form1.List1.AddItem "wParam = DBT_DEVNODES_CHANGED"
Case DBT_QUERYCHANGECONFIG
Form1.List1.AddItem "wParam = DBT_QUERYCHANGECONFIG"
Case DBT_USERDEFINED
Form1.List1.AddItem "wParam = DBT_USERDEFINED"
Case Else
Form1.List1.AddItem "wParam = unknown = " & wParam
End Select
End Select
' pass the rest messages onto VB's own Window Procedure
MyWindowProc = CallWindowProc(glngPrevWndProc, hWnd, Msg, wParam, lParam)
End Function
Public Function DoRegisterDeviceInterface(hWnd As Long, ByRef hDevNotify As Long) As Boolean
Dim NotificationFilter As DEV_BROADCAST_DEVICEINTERFACE
NotificationFilter.dbcc_size = Len(NotificationFilter)
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
hDevNotify = RegisterDeviceNotification(hWnd, NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE Or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)
If hDevNotify = 0 Then
MsgBox "RegisterDeviceNotification failed: " & CStr(Err.LastDllError), vbOKOnly
DoRegisterDeviceInterface = False
Exit Function
End If
DoRegisterDeviceInterface = True
End Function
BTW... This is my first post here so hi everyone
-
Feb 1st, 2010, 12:22 PM
#4
New Member
Re: Detect USB Flsh Drive
Hello pcuser,
I wonder how some people ask here in forums, get an excellent example as this that you posted and then go away without even say thanks....
I really wanted to thank you for sharing this post as it really helped me right now
and has all the details I needed for my program.
This is my first post here and I just registered to thank you, and why not, will return here from time to time...
It looks a good vb resource here. Wish you all the best!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|