求“禁用u盘”的vb.net2005代码

   阅读
求“禁用u盘”的vb.net2005代码!
已经有了vb6的代码,但不知如何升级为vb.net2005的代码:
VB.NET code
'本程序需要在主窗体外添加一个“警告窗体”和一个模块
'主窗体代码
Option Explicit
Private Sub Form_Load()
'子类化窗体的消息处理函数
HookForm Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
'程序退出时恢复原窗体处理函数
UnHookForm Me
End Sub

VB.NET code
'MODULE1
'模块代码:
Option Explicit
'子类化窗体消息处理函数时需要使用的API,很常见,不作过多说明。
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Const GWL_WNDPROC = -4
Const WM_DEVICECHANGE As Long = &H219
Const DBT_DEVICEARRIVAL As Long = &H8000&
Const DBT_DEVICEREMOVECOMPLETE As Long = &H8004&

Public PrevProc As Long      '原来的窗体消息处理函数地址
Public Sub HookForm(F As Form)
        PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHookForm(F As Form)
        SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Select Case uMsg
            '插入USB DISK 则接收到此消息
        Case WM_DEVICECHANGE
            If wParam = DBT_DEVICEARRIVAL Then
                Formusb.Show '跳出警告窗体
            End If
            If wParam = DBT_DEVICEREMOVECOMPLETE Then
                Unload Formusb  '警告窗体消失
            End If
        End Select
        ' 调用原来的窗体消息处理函数
        WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
End Function
'***************************************************
'***************************************************



------解决方案--------------------
API 定义把 Long 修改成 Integer
------解决方案--------------------
Declare Auto Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

改成这个API试试

VB.NET code

 '抽掉钩子   
    Public Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer) As Boolean
Dim retKeyboard As Boolean = True
retKeyboard = UnhookWindowsHookEx(PrevProc)

------解决方案--------------------
VB.NET code
Public Class Form1

    Const WM_DEVICECHANGE As Int32 = &H219
    Const DBT_DEVICEARRIVAL As Int32 = &H8000&
    Const DBT_DEVICEREMOVECOMPLETE As Int32 = &H8004&

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg

            Case WM_DEVICECHANGE
                If m.WParam = DBT_DEVICEARRIVAL Then
                    ' Formusb.Show() '跳出警告窗体
                End If
                If m.WParam = DBT_DEVICEREMOVECOMPLETE Then
                    'Unload(Formusb)  '警告窗体消失
                End If

        End Select
        MyBase.WndProc(m)
    End Sub
End Class
阅读