VB6下的大难题呀,两天了,有谁能帮帮我……懂USB接口的请进。高分悬赏!
'===========标准模式============
Option Explicit
Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type '固定常数
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 '规定的常数
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 '规定的常数
Const GWL_WNDPROC = -4
Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Const WM_DEVICECHANGE = &H219&
Const DBT_DEVTYP_DEVICEINTERFACE = &H5&
Const DBT_DEVTYP_VOLUME = &H2&
Const DBT_DEVICEARRIVAL = &H8000&
Const DBT_DEVICEREMOVECOMPLETE = &H8004&
Const DRIVE_REMOVABLE = 2
Const DRIVE_NO_ROOT_DIR = 1
Declare Function SetWindowLongA Lib "User32.dll " (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProcA Lib "User32.dll " (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterDeviceNotificationA Lib "User32.dll " (ByVal hRecipient As Long, NotificationFilter As Any, ByVal Flags As Long) As Long
Declare Function UnregisterDeviceNotification Lib "User32.dll " (ByVal Handle As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32 " (Destination As Any, Source As Any, ByVal Length As Long)
Private m_hwnd As Long
Private m_lpPrevWndProc As Long
Private m_hDevNotify As Long
Public Sub RegDevNotify(ByVal hwnd As Long) '注册钩子
Dim dbcc As DEV_BROADCAST_DEVICEINTERFACE
If m_lpPrevWndProc = 0 Then
m_hwnd = hwnd
m_lpPrevWndProc = SetWindowLongA(m_hwnd, GWL_WNDPROC, AddressOf WndProc)
dbcc.dbcc_size = Len(dbcc)
dbcc.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
m_hDevNotify = RegisterDeviceNotificationA(hwnd, dbcc, DEVICE_NOTIFY_WINDOW_HANDLE)
End If
End Sub
Public Sub UnregDevNotify() '解除钩子
If m_lpPrevWndProc Then
UnregisterDeviceNotification m_hDevNotify
SetWindowLongA m_hwnd, GWL_WNDPROC, m_lpPrevWndProc
m_lpPrevWndProc = 0
End If
End Sub
Private Function WndProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '消息拦截 当有消息时候会在这里出现
If msg = WM_DEVICECHANGE Then '当消息是 磁盘发生改变
Debug.Print msg '打印变量值
Debug.Print wParam '打印变量值
Debug.Print lParam '打印变量值
If wParam = DBT_DEVICEARRIVAL Or wParam = DBT_DEVICEREMOVECOMPLETE Then '当标志是插入一个磁盘 或者 移动完成
MsgBox ("USB Changed !!")
End If
End If
WndProc = CallWindowProcA(m_lpPrevWndProc, m_hwnd, msg, wParam, lParam) '返回消息给系统 否则系统所有消息都被拦截 你的电脑几乎就是废了 关机都不行
End Function
'===============调用==============
Sub Form_Load()
RegDevNotify Me.hwnd '向系统注册一个拦截优盘状态的钩子
End Sub
Sub Form_Unload(Cancel As Integer)
UnregDevNotify '解除钩子
End Sub
End Type '固定常数
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 '规定的常数
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 '规定的常数
Public Const GWL_WNDPROC = -4
Const DEVICE_NOTIFY_WINDOW_HANDLE = 0
Const WM_DEVICECHANGE = &H219&
Const DBT_DEVTYP_DEVICEINTERFACE = &H5&
Const DBT_DEVTYP_VOLUME = &H2&
Const DBT_DEVICEARRIVAL = &H8000&
Const DBT_DEVICEREMOVECOMPLETE = &H8004&
Const DRIVE_REMOVABLE = 2
Const DRIVE_NO_ROOT_DIR = 1
Public Declare Function SetWindowLongA Lib "User32.dll " (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProcA Lib "User32.dll " (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterDeviceNotificationA Lib "User32.dll " (ByVal hRecipient As Long, NotificationFilter As Any, ByVal Flags As Long) As Long
Declare Function UnregisterDeviceNotification Lib "User32.dll " (ByVal Handle As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32 " (Destination As Any, Source As Any, ByVal Length As Long)
Private m_hwnd As Long
Private m_lpPrevWndProc As Long
Private m_hDevNotify As Long
Public lpPrev As Long
'Public Sub RegDevNotify(ByVal hwnd As Long) '注册钩子
' Dim dbcc As DEV_BROADCAST_DEVICEINTERFACE
' If m_lpPrevWndProc = 0 Then
' m_hwnd = hwnd
' m_lpPrevWndProc = SetWindowLongA(m_hwnd, GWL_WNDPROC, AddressOf WndProc)
' dbcc.dbcc_size = Len(dbcc)
' dbcc.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
' m_hDevNotify = RegisterDeviceNotificationA(hwnd, dbcc, DEVICE_NOTIFY_WINDOW_HANDLE)
' End If
'End Sub
'
'Public Sub UnregDevNotify() '解除钩子
' If m_lpPrevWndProc Then
' UnregisterDeviceNotification m_hDevNotify
' SetWindowLongA m_hwnd, GWL_WNDPROC, m_lpPrevWndProc
' m_lpPrevWndProc = 0
' End If
'End Sub
Public Function WndProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '消息拦截 当有消息时候会在这里出现
If msg = WM_DEVICECHANGE Then '当消息是 磁盘发生改变
Debug.Print msg '打印变量值[/color]
Debug.Print wParam '打印变量值[/color]
Debug.Print lParam '打印变量值[/color]
' If wParam = DBT_DEVICEARRIVAL Or wParam = DBT_DEVICEREMOVECOMPLETE Then '当标志是插入一个磁盘 或者 移动完成
' MsgBox ("USB Changed !!")
' End If
End If
WndProc = CallWindowProcA(lpPrev, hwnd, msg, wParam, lParam) '返回消息给系统 否则系统所有消息都被拦截 你的电脑几乎就是废了 关机都不行
End Function
''===============调用==============
'Sub Form_Load()
' RegDevNotify Me.hwnd '向系统注册一个拦截优盘状态的钩子
'End Sub
'
'Sub Form_Unload(Cancel As Integer)
' UnregDevNotify '解除钩子
'End Sub
'窗口代码
Option Explicit
Private Sub Form_Load()
lpPrev = SetWindowLongA(hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
If lpPrev <> 0 Then
lpPrev = SetWindowLongA(hwnd, GWL_WNDPROC, lpPrev)
lpPrev = 0
End If
End Sub