Click here to Skip to main content
15,896,288 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

VB
#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()]
Posted
Comments
Sergey Alexandrovich Kryukov 4-Jun-14 12:21pm    
It usually means that you have a race condition. Timing is different, so your buggy behavior gives different results at different timing. Why hooking a mouse at all? It's better to avoid using P/Invoke at all and keep it with pure .NET FCL.
—SA
[no name] 4-Jun-14 15:29pm    
I managed to get BlockInput() from user32.dll to work, thanks for helping.
Sergey Alexandrovich Kryukov 4-Jun-14 15:34pm    
You are welcome.
—SA

1 solution

Imports System.Runtime.InteropServices
...
<dllimport("user32.dll",> _
Public Shared Function BlockInput(<[In](), MarshalAs(UnmanagedType.Bool)> ByVal fBlockIt As Boolean) As <marshalas(unmanagedtype.bool)> Boolean
End Function


'To block mouse and keyboard:
'BlockInput(True)

'To unblock mouse and keyboard
'BlockInput(False)

>>>MUST BE RUN AS ADMINISTRATOR<<<</xml>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900