Skip to content

Commit a1ed0bf

Browse files
Fixed OverflowException in the LocalKeyboardHook.
1 parent e109484 commit a1ed0bf

2 files changed

Lines changed: 104 additions & 38 deletions

File tree

InputHelper Library/InputHelper .NET 3.5/InputHelper.vb

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,11 @@ Public NotInheritable Class InputHelper
541541

542542
If nCode >= NativeMethods.HookCode.HC_ACTION Then
543543
Dim KeyCode As Keys = CType(wParam.ToInt32(), Keys)
544-
Dim KeyFlags As New NativeMethods.DWORD(lParam.ToInt64())
544+
Dim KeyFlags As New NativeMethods.QWORD(lParam.ToInt64())
545545
Dim ScanCode As Byte = BitConverter.GetBytes(KeyFlags)(2) 'The scan code is the third byte in the integer (bits 16-23).
546-
Dim Extended As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_EXTENDED) = NativeMethods.KeyboardFlags.KF_EXTENDED
547-
Dim AltDown As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_ALTDOWN) = NativeMethods.KeyboardFlags.KF_ALTDOWN
548-
Dim KeyUp As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_UP) = NativeMethods.KeyboardFlags.KF_UP
546+
Dim Extended As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_EXTENDED) = NativeMethods.KeyboardFlags.KF_EXTENDED
547+
Dim AltDown As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_ALTDOWN) = NativeMethods.KeyboardFlags.KF_ALTDOWN
548+
Dim KeyUp As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_UP) = NativeMethods.KeyboardFlags.KF_UP
549549

550550
'Set the ALT modifier if the KF_ALTDOWN flag is set.
551551
If AltDown = True _
@@ -772,15 +772,15 @@ Public NotInheritable Class InputHelper
772772

773773

774774
Case NativeMethods.MouseMessage.WM_MOUSEWHEEL
775-
Dim Delta As Integer = New NativeMethods.SignedDWORD(MouseEventInfo.mouseData).High
775+
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
776776
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
777777
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Vertical, Delta)
778778
RaiseEvent MouseWheel(Me, HookEventArgs)
779779
Block = HookEventArgs.Block
780780

781781

782782
Case NativeMethods.MouseMessage.WM_MOUSEHWHEEL
783-
Dim Delta As Integer = New NativeMethods.SignedDWORD(MouseEventInfo.mouseData).High
783+
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
784784
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
785785
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Horizontal, Delta)
786786
RaiseEvent MouseWheel(Me, HookEventArgs)
@@ -1333,11 +1333,11 @@ Public NotInheritable Class InputHelper
13331333
End If
13341334

13351335
Dim wParam As IntPtr = IntPtr.Zero 'Used to specify which X button was clicked (if any).
1336-
Dim lParam As IntPtr = New NativeMethods.DWORD(ClickPoint.x, ClickPoint.y) 'Click point.
1336+
Dim lParam As IntPtr = New NativeMethods.DWORD(CType(ClickPoint.x And Short.MaxValue, Short), CType(ClickPoint.y And Short.MaxValue, Short)) 'Click point.
13371337

13381338
If Button = MouseButtons.XButton1 OrElse _
13391339
Button = MouseButtons.XButton2 Then
1340-
wParam = New NativeMethods.DWORD(0, Button / MouseButtons.XButton1) 'Set the correct XButton.
1340+
wParam = New NativeMethods.DWORD(0US, CType(Button / MouseButtons.XButton1, UShort)) 'Set the correct XButton.
13411341
End If
13421342

13431343
InputHelper.WindowMessages.SendMessage(hWnd, ButtonMessage, wParam, lParam, SendAsynchronously)
@@ -2040,46 +2040,79 @@ Public NotInheritable Class InputHelper
20402040
<FieldOffset(0)> Public Low As UShort
20412041
<FieldOffset(2)> Public High As UShort
20422042

2043+
<FieldOffset(0)> Public SignedValue As Integer
2044+
<FieldOffset(0)> Public SignedLow As Short
2045+
<FieldOffset(2)> Public SignedHigh As Short
2046+
20432047
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As IntPtr
2044-
Return New IntPtr(DWORD.Value And Integer.MaxValue)
2048+
Return New IntPtr(DWORD.SignedValue)
20452049
End Operator
20462050

20472051
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As UInteger
20482052
Return DWORD.Value
20492053
End Operator
20502054

2055+
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As Integer
2056+
Return DWORD.SignedValue
2057+
End Operator
2058+
20512059
Public Sub New(ByVal Value As UInteger)
20522060
Me.Value = Value
20532061
End Sub
20542062

2063+
Public Sub New(ByVal Value As Integer)
2064+
Me.SignedValue = Value
2065+
End Sub
2066+
20552067
Public Sub New(ByVal Low As UShort, ByVal High As UShort)
20562068
Me.Low = Low
20572069
Me.High = High
20582070
End Sub
2071+
2072+
Public Sub New(ByVal Low As Short, ByVal High As Short)
2073+
Me.SignedLow = Low
2074+
Me.SignedHigh = High
2075+
End Sub
20592076
End Structure
20602077

20612078
<StructLayout(LayoutKind.Explicit)>
2062-
Public Structure SignedDWORD
2063-
<FieldOffset(0)> Public Value As UInteger
2064-
<FieldOffset(0)> Public Low As Short
2065-
<FieldOffset(2)> Public High As Short
2079+
Public Structure QWORD
2080+
<FieldOffset(0)> Public Value As ULong
2081+
<FieldOffset(0)> Public Low As UInteger
2082+
<FieldOffset(4)> Public High As UInteger
2083+
2084+
<FieldOffset(0)> Public SignedValue As Long
2085+
<FieldOffset(0)> Public SignedLow As Integer
2086+
<FieldOffset(4)> Public SignedHigh As Integer
20662087

2067-
Public Shared Widening Operator CType(ByVal DWORD As SignedDWORD) As IntPtr
2068-
Return New IntPtr(DWORD.Value And Integer.MaxValue)
2088+
<FieldOffset(0)> Public LowWord As DWORD
2089+
<FieldOffset(4)> Public HighWord As DWORD
2090+
2091+
Public Shared Widening Operator CType(ByVal QWORD As QWORD) As ULong
2092+
Return QWORD.Value
20692093
End Operator
20702094

2071-
Public Shared Widening Operator CType(ByVal DWORD As SignedDWORD) As UInteger
2072-
Return DWORD.Value
2095+
Public Shared Widening Operator CType(ByVal QWORD As QWORD) As Long
2096+
Return QWORD.SignedValue
20732097
End Operator
20742098

2075-
Public Sub New(ByVal Value As UInteger)
2099+
Public Sub New(ByVal Value As ULong)
20762100
Me.Value = Value
20772101
End Sub
20782102

2079-
Public Sub New(ByVal Low As UShort, ByVal High As UShort)
2103+
Public Sub New(ByVal Value As Long)
2104+
Me.SignedValue = Value
2105+
End Sub
2106+
2107+
Public Sub New(ByVal Low As UInteger, ByVal High As UInteger)
20802108
Me.Low = Low
20812109
Me.High = High
20822110
End Sub
2111+
2112+
Public Sub New(ByVal Low As Integer, ByVal High As Integer)
2113+
Me.SignedLow = Low
2114+
Me.SignedHigh = High
2115+
End Sub
20832116
End Structure
20842117

20852118
<StructLayout(LayoutKind.Explicit)> _

InputHelper Library/InputHelper .NET 4.x/InputHelper.vb

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,11 @@ Public NotInheritable Class InputHelper
541541

542542
If nCode >= NativeMethods.HookCode.HC_ACTION Then
543543
Dim KeyCode As Keys = CType(wParam.ToInt32(), Keys)
544-
Dim KeyFlags As New NativeMethods.DWORD(lParam.ToInt64())
544+
Dim KeyFlags As New NativeMethods.QWORD(lParam.ToInt64())
545545
Dim ScanCode As Byte = BitConverter.GetBytes(KeyFlags)(2) 'The scan code is the third byte in the integer (bits 16-23).
546-
Dim Extended As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_EXTENDED) = NativeMethods.KeyboardFlags.KF_EXTENDED
547-
Dim AltDown As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_ALTDOWN) = NativeMethods.KeyboardFlags.KF_ALTDOWN
548-
Dim KeyUp As Boolean = (KeyFlags.High And NativeMethods.KeyboardFlags.KF_UP) = NativeMethods.KeyboardFlags.KF_UP
546+
Dim Extended As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_EXTENDED) = NativeMethods.KeyboardFlags.KF_EXTENDED
547+
Dim AltDown As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_ALTDOWN) = NativeMethods.KeyboardFlags.KF_ALTDOWN
548+
Dim KeyUp As Boolean = (KeyFlags.LowWord.High And NativeMethods.KeyboardFlags.KF_UP) = NativeMethods.KeyboardFlags.KF_UP
549549

550550
'Set the ALT modifier if the KF_ALTDOWN flag is set.
551551
If AltDown = True _
@@ -772,15 +772,15 @@ Public NotInheritable Class InputHelper
772772

773773

774774
Case NativeMethods.MouseMessage.WM_MOUSEWHEEL
775-
Dim Delta As Integer = New NativeMethods.SignedDWORD(MouseEventInfo.mouseData).High
775+
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
776776
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
777777
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Vertical, Delta)
778778
RaiseEvent MouseWheel(Me, HookEventArgs)
779779
Block = HookEventArgs.Block
780780

781781

782782
Case NativeMethods.MouseMessage.WM_MOUSEHWHEEL
783-
Dim Delta As Integer = New NativeMethods.SignedDWORD(MouseEventInfo.mouseData).High
783+
Dim Delta As Integer = New NativeMethods.DWORD(MouseEventInfo.mouseData).SignedHigh
784784
Dim HookEventArgs As New MouseHookEventArgs(MouseButtons.None, KeyState.Up, False, _
785785
New Point(MouseEventInfo.pt.x, MouseEventInfo.pt.y), ScrollDirection.Horizontal, Delta)
786786
RaiseEvent MouseWheel(Me, HookEventArgs)
@@ -1333,11 +1333,11 @@ Public NotInheritable Class InputHelper
13331333
End If
13341334

13351335
Dim wParam As IntPtr = IntPtr.Zero 'Used to specify which X button was clicked (if any).
1336-
Dim lParam As IntPtr = New NativeMethods.DWORD(ClickPoint.x, ClickPoint.y) 'Click point.
1336+
Dim lParam As IntPtr = New NativeMethods.DWORD(CType(ClickPoint.x And Short.MaxValue, Short), CType(ClickPoint.y And Short.MaxValue, Short)) 'Click point.
13371337

13381338
If Button = MouseButtons.XButton1 OrElse _
13391339
Button = MouseButtons.XButton2 Then
1340-
wParam = New NativeMethods.DWORD(0, Button / MouseButtons.XButton1) 'Set the correct XButton.
1340+
wParam = New NativeMethods.DWORD(0US, CType(Button / MouseButtons.XButton1, UShort)) 'Set the correct XButton.
13411341
End If
13421342

13431343
InputHelper.WindowMessages.SendMessage(hWnd, ButtonMessage, wParam, lParam, SendAsynchronously)
@@ -2040,46 +2040,79 @@ Public NotInheritable Class InputHelper
20402040
<FieldOffset(0)> Public Low As UShort
20412041
<FieldOffset(2)> Public High As UShort
20422042

2043+
<FieldOffset(0)> Public SignedValue As Integer
2044+
<FieldOffset(0)> Public SignedLow As Short
2045+
<FieldOffset(2)> Public SignedHigh As Short
2046+
20432047
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As IntPtr
2044-
Return New IntPtr(DWORD.Value And Integer.MaxValue)
2048+
Return New IntPtr(DWORD.SignedValue)
20452049
End Operator
20462050

20472051
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As UInteger
20482052
Return DWORD.Value
20492053
End Operator
20502054

2055+
Public Shared Widening Operator CType(ByVal DWORD As DWORD) As Integer
2056+
Return DWORD.SignedValue
2057+
End Operator
2058+
20512059
Public Sub New(ByVal Value As UInteger)
20522060
Me.Value = Value
20532061
End Sub
20542062

2063+
Public Sub New(ByVal Value As Integer)
2064+
Me.SignedValue = Value
2065+
End Sub
2066+
20552067
Public Sub New(ByVal Low As UShort, ByVal High As UShort)
20562068
Me.Low = Low
20572069
Me.High = High
20582070
End Sub
2071+
2072+
Public Sub New(ByVal Low As Short, ByVal High As Short)
2073+
Me.SignedLow = Low
2074+
Me.SignedHigh = High
2075+
End Sub
20592076
End Structure
20602077

20612078
<StructLayout(LayoutKind.Explicit)>
2062-
Public Structure SignedDWORD
2063-
<FieldOffset(0)> Public Value As UInteger
2064-
<FieldOffset(0)> Public Low As Short
2065-
<FieldOffset(2)> Public High As Short
2079+
Public Structure QWORD
2080+
<FieldOffset(0)> Public Value As ULong
2081+
<FieldOffset(0)> Public Low As UInteger
2082+
<FieldOffset(4)> Public High As UInteger
2083+
2084+
<FieldOffset(0)> Public SignedValue As Long
2085+
<FieldOffset(0)> Public SignedLow As Integer
2086+
<FieldOffset(4)> Public SignedHigh As Integer
20662087

2067-
Public Shared Widening Operator CType(ByVal DWORD As SignedDWORD) As IntPtr
2068-
Return New IntPtr(DWORD.Value And Integer.MaxValue)
2088+
<FieldOffset(0)> Public LowWord As DWORD
2089+
<FieldOffset(4)> Public HighWord As DWORD
2090+
2091+
Public Shared Widening Operator CType(ByVal QWORD As QWORD) As ULong
2092+
Return QWORD.Value
20692093
End Operator
20702094

2071-
Public Shared Widening Operator CType(ByVal DWORD As SignedDWORD) As UInteger
2072-
Return DWORD.Value
2095+
Public Shared Widening Operator CType(ByVal QWORD As QWORD) As Long
2096+
Return QWORD.SignedValue
20732097
End Operator
20742098

2075-
Public Sub New(ByVal Value As UInteger)
2099+
Public Sub New(ByVal Value As ULong)
20762100
Me.Value = Value
20772101
End Sub
20782102

2079-
Public Sub New(ByVal Low As UShort, ByVal High As UShort)
2103+
Public Sub New(ByVal Value As Long)
2104+
Me.SignedValue = Value
2105+
End Sub
2106+
2107+
Public Sub New(ByVal Low As UInteger, ByVal High As UInteger)
20802108
Me.Low = Low
20812109
Me.High = High
20822110
End Sub
2111+
2112+
Public Sub New(ByVal Low As Integer, ByVal High As Integer)
2113+
Me.SignedLow = Low
2114+
Me.SignedHigh = High
2115+
End Sub
20832116
End Structure
20842117

20852118
<StructLayout(LayoutKind.Explicit)> _

0 commit comments

Comments
 (0)