1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
|
'
' // Module1.bas
'
Option Explicit
'egl1044
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_DEVICECHANGE As Long = &H219
Private Const DBT_DEVICEARRIVAL As Long = 32768
Private Const DBT_DEVICEREMOVECOMPLETE As Long = 32772
Private Const DBT_DEVTYP_DEVICEINTERFACE As Long = &H5&
Private Const DBT_DEVTYP_VOLUME As Long = &H2&
Private Const GUID_USB As String = "{A5DCBF10-6530-11D2-901F-00C04FB951ED}"
Private Type UUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type DEV_BROADCAST_HDR
dbch_size As Long
dbch_devicetype As Long
dbch_reserved As Long
End Type
Private Type DEV_BROADCAST_DEVICEINTERFACE
dbcc_size As Long
dbcc_devicetype As Long
dbcc_reserved As Long
dbcc_classguid As UUID
dbcc_name As Long
End Type
Private Type DEV_BROADCAST_VOLUME
dbcv_size As Long
dbcv_devicetype As Long
dbcv_reserved As Long
dbcv_unitmask As Long
dbcv_flags As Integer
End Type
Private Declare Function CallWindowProcW Lib "User32.dll" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function SetWindowLongW Lib "User32.dll" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function RegisterDeviceNotificationW Lib "User32.dll" ( _
ByVal hRecipient As Long, _
ByVal NotificationFilter As Long, _
ByVal Flags As Long) As Long
Private Declare Function UnregisterDeviceNotification Lib "User32.dll" ( _
ByVal Handle As Long) As Long
Private Declare Function CLSIDFromString Lib "Ole32.dll" ( _
ByVal lpsz As Long, _
ByRef pclsid As UUID) As Long
Private Declare Sub RtlMoveMemory Lib "Kernel32.dll" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Private Declare Sub RtlZeroMemory Lib "Kernel32.dll" ( _
ByRef dst As Any, _
ByVal nBytes As Long)
Public func As Long
Public hDev As Long
Public hWindow As Long
Dim lpdb As DEV_BROADCAST_HDR
Dim lpdbv As DEV_BROADCAST_VOLUME
Dim lpdbdi As DEV_BROADCAST_DEVICEINTERFACE
Dim fUsb As Boolean
Private Function GetDriveFromMask(ByVal UnitMask As Long) As String
GetDriveFromMask = Chr$(65 + (Log(UnitMask) / Log(2)))
End Function
Public Sub RegisterDeviceInterfaceToHwnd(ByVal hWnd As Long)
Dim lpdbd As DEV_BROADCAST_DEVICEINTERFACE
Dim lpusb As UUID
If func <> 0 Then
Exit Sub
End If
hWindow = hWnd
CLSIDFromString StrPtr(GUID_USB), lpusb
lpdbd.dbcc_size = Len(lpdbd)
lpdbd.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
lpdbd.dbcc_classguid = lpusb
hDev = RegisterDeviceNotificationW(hWindow, VarPtr(lpdbd), 0)
func = SetWindowLongW(hWindow, GWL_WNDPROC, AddressOf OnUsbChange)
End Sub
Public Sub UnRegisterDeviceInterface()
UnregisterDeviceNotification hDev
func = SetWindowLongW(hWindow, GWL_WNDPROC, func)
func = 0
hWindow = 0
End Sub
Public Function OnUsbChange(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_DEVICECHANGE Then
Select Case wParam
Case DBT_DEVICEARRIVAL
RtlMoveMemory lpdb, ByVal lParam&, Len(lpdb)
If lpdb.dbch_devicetype = DBT_DEVTYP_DEVICEINTERFACE Then
RtlMoveMemory lpdbdi, ByVal lParam&, Len(lpdbdi)
fUsb = True
End If
If lpdb.dbch_devicetype = DBT_DEVTYP_VOLUME And fUsb Then
RtlMoveMemory lpdbv, ByVal lParam&, Len(lpdbv)
fUsb = False
' Inserted
Debug.Print "Inserted @ drive= " & GetDriveFromMask(lpdbv.dbcv_unitmask)
End If
Case DBT_DEVICEREMOVECOMPLETE
RtlMoveMemory lpdb, ByVal lParam&, Len(lpdb)
If lpdb.dbch_devicetype = DBT_DEVTYP_DEVICEINTERFACE Then
RtlMoveMemory lpdbdi, ByVal lParam&, Len(lpdbdi)
fUsb = True
End If
If lpdb.dbch_devicetype = DBT_DEVTYP_VOLUME And fUsb Then
RtlMoveMemory lpdbv, ByVal lParam&, Len(lpdbv)
fUsb = False
' Removed
Debug.Print "Removed @ drive= " & GetDriveFromMask(lpdbv.dbcv_unitmask)
End If
End Select
End If
OnUsbChange = CallWindowProcW(func, hWnd, uMsg, wParam, lParam)
End Function
'
' // Form1.frm
'
Option Explicit
Private Sub Form_Load()
Call RegisterDeviceInterfaceToHwnd(Me.hWnd)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call UnRegisterDeviceInterface
End Sub
|