Hello,
I have a code that hooks the mouse and effectively blocks it. When I debug it in the VB Express, once I've unchecked "Enable the Visual Studio hosting process" in My Project-->Debug, it runs fine. But when I compile the application and run the executable, as soon as I move the mouse, I get an appcrash. Please tell me why!
Code:
#Region "Mouse"
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelMouseProcDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSLLHOOKSTRUCT) As Integer
Private Delegate Function LowLevelMouseProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSLLHOOKSTRUCT) As Integer
Private hhkLowLevelMouse As Integer
Private Structure POINT
Private x As Integer
Private y As Integer
End Structure
Private Structure MSLLHOOKSTRUCT
Private pt As POINT
Private mouseData As Integer
Private flags As Integer
Private time As Integer
Private dwExtraInfo As Integer
End Structure
Private Function LowLevelMouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSLLHOOKSTRUCT) As Integer
If (nCode = 0) Then
If wParam = &H200 Or wParam = &H201 Or wParam = &H202 Or wParam = &H203 Or wParam = &H204 Or wParam = &H205 Or wParam = &H206 Or wParam = &H207 Or wParam = &H208 Or wParam = &H209 Or wParam = &H20A Then
Return 1
End If
Return CallNextHookEx(hhkLowLevelMouse, nCode, wParam, lParam)
End If
Return Nothing
End Function
Public Sub DisableMouse()
hhkLowLevelMouse = SetWindowsHookEx(14, AddressOf LowLevelMouseProc, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub
Public Sub EnableMouse()
UnhookWindowsHookEx(hhkLowLevelMouse)
End Sub
#End Region
[To block: DisableMouse(); to unblock: EnableMouse()]