Seems to me that this code crashes on 64-bit machines. The call to ToInt32 throws OverflowException in some cases (say, mouse back button).
The short version:
If your CPU is 32 bit and your OS is 32 bit, use an Int32 for any API value not explicitly specified as something else.
If your CPU is 64 bit and your OS is 32 bit, use an Int32 for any API value not explicitly specified as something else.
If your CPU is 64 bit and your OS is also 64 bit, use an Int64 for any API value not explicitly specified as something else.
If your CPU is 32 bit and your OS is 64 bit, tell me how you managed that!
The long version:
Most of what comes back from Windows API Parameters is DWORD. Usually the documentation specifies this, but sometimes not. Where not explicitly specified as something, always assume DWORD.
A DWORD is always equivalent to a runtime-system native WORD, which means that on an x86 Platform it's 32 bit and on an x64 it's 64 bit.
Your simplest solution looks like this:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
'The SystemBitCount Size provided by the OS.
'If BitVal is 8, system is x64 (64 bit).
'If BitVal is 4, system is x86 (32 bit).
Dim SystemBitCount As System.Int32 = System.IntPtr.Size
'Use an Object instead of an explicitly bounded integer word
Dim cmd As System.Object = Nothing
If m.Msg = &H319 Then ' WM_APPCOMMAND
If SystemBitCount = 4 Then
cmd = CType(((m.LParam.ToInt32() >> 16) And &HFFF), System.Object)
ElseIf SystemBitCount = 8 Then
cmd = CType(((m.LParam.ToInt64() >> 16) And &HFFF), System.Object)
End If
'--- Do your stuff
'...
'When-or-where-ever you need to employ this object-cast value in code...
If SystemBitCount = 4 Then
Dim MyOutputValue As System.Int32 = CType(cmd, System.Int32)
ElseIf SystemBitCount = 8 Then
Dim MyOutputValue As System.Int64 = CType(cmd, System.Int64)
End If
End If
MyBase.WndProc(m)
End Sub
It never hurts to try. In a worst case scenario, you'll learn from it.