Windows hook
-
Looking to create a global hook to intercept WM_WINDOWPOSCHANGED messages. So far all attempts to create a hook always end up not functioning properly. I create the hook then filter out messages that aren't what I'm looking for, but it always seems to make all non-system processes that are running crash. My Code:
Public Class WM_WINDOWPOSCHANGED
Shared Event WindowMoving(ByVal handle As IntPtr)
Shared hookhandle As Integer
Shared hookhandler As HookHandler
Private Sub New()End Sub Public Shared Sub CreateTheHook() 'If hookhandle = 0 Then ' hookhandle = User32Imports.SetWindowsHookEx(CInt(HookCodes.WH\_CALLWNDPROC), hookhandler, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), CInt(0)) ' MsgBox(hookhandle) 'End If hookhandler = New HookHandler(AddressOf ChangeButtonHost) hookhandle = User32Imports.CreateSystemHook(Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), hookhandler) If hookhandle <> 0 Then MsgBox("hi") End Sub Shared Function ChangeButtonHost(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) If nCode >= 0 Then Dim cwps As CWPSTRUCT = DirectCast(System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, GetType(CWPSTRUCT)), CWPSTRUCT) Dim winfo As New WindowInfo(cwps.hwnd) If cwps.message = &H46 And WindowInfo.IsValid(cwps.hwnd) And winfo.ShowInTaskbar = True Then RaiseEvent WindowMoving(cwps.hwnd) End If End If Return 0 'User32Imports.CallNextHookEx(hookhandle, nCode, wParam, lParam) End Function End Class
Friend Class User32Imports
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookHandler, ByVal hInstance As IntPtr, ByVal ThreadId As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lparam As IntPtr) As Integer
End FunctionEnd Class>
EDIT: CreateSystemHook was my attempt at doing the
-
Looking to create a global hook to intercept WM_WINDOWPOSCHANGED messages. So far all attempts to create a hook always end up not functioning properly. I create the hook then filter out messages that aren't what I'm looking for, but it always seems to make all non-system processes that are running crash. My Code:
Public Class WM_WINDOWPOSCHANGED
Shared Event WindowMoving(ByVal handle As IntPtr)
Shared hookhandle As Integer
Shared hookhandler As HookHandler
Private Sub New()End Sub Public Shared Sub CreateTheHook() 'If hookhandle = 0 Then ' hookhandle = User32Imports.SetWindowsHookEx(CInt(HookCodes.WH\_CALLWNDPROC), hookhandler, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), CInt(0)) ' MsgBox(hookhandle) 'End If hookhandler = New HookHandler(AddressOf ChangeButtonHost) hookhandle = User32Imports.CreateSystemHook(Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), hookhandler) If hookhandle <> 0 Then MsgBox("hi") End Sub Shared Function ChangeButtonHost(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) If nCode >= 0 Then Dim cwps As CWPSTRUCT = DirectCast(System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, GetType(CWPSTRUCT)), CWPSTRUCT) Dim winfo As New WindowInfo(cwps.hwnd) If cwps.message = &H46 And WindowInfo.IsValid(cwps.hwnd) And winfo.ShowInTaskbar = True Then RaiseEvent WindowMoving(cwps.hwnd) End If End If Return 0 'User32Imports.CallNextHookEx(hookhandle, nCode, wParam, lParam) End Function End Class
Friend Class User32Imports
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookHandler, ByVal hInstance As IntPtr, ByVal ThreadId As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lparam As IntPtr) As Integer
End FunctionEnd Class>
EDIT: CreateSystemHook was my attempt at doing the
It's not going to work. You can't implement system-wide hooks inside managed code. You'll have to write a C++ "normal" .DLL to handle the hook, then expose an itnerface you can use to talk to managed code, like C# or VB.NET. .NET does not support the exports required of a .DLL to support a system-wide hook. It'll work for a "local" hook. That is, hooking your own application, but not system-wide. You can find Microsoft's documentation on it here[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
It's not going to work. You can't implement system-wide hooks inside managed code. You'll have to write a C++ "normal" .DLL to handle the hook, then expose an itnerface you can use to talk to managed code, like C# or VB.NET. .NET does not support the exports required of a .DLL to support a system-wide hook. It'll work for a "local" hook. That is, hooking your own application, but not system-wide. You can find Microsoft's documentation on it here[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks for the response. I've looked at the MSDN docs for hooks, and created a C++ version, but I'm getting the same results.
-
Looking to create a global hook to intercept WM_WINDOWPOSCHANGED messages. So far all attempts to create a hook always end up not functioning properly. I create the hook then filter out messages that aren't what I'm looking for, but it always seems to make all non-system processes that are running crash. My Code:
Public Class WM_WINDOWPOSCHANGED
Shared Event WindowMoving(ByVal handle As IntPtr)
Shared hookhandle As Integer
Shared hookhandler As HookHandler
Private Sub New()End Sub Public Shared Sub CreateTheHook() 'If hookhandle = 0 Then ' hookhandle = User32Imports.SetWindowsHookEx(CInt(HookCodes.WH\_CALLWNDPROC), hookhandler, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), CInt(0)) ' MsgBox(hookhandle) 'End If hookhandler = New HookHandler(AddressOf ChangeButtonHost) hookhandle = User32Imports.CreateSystemHook(Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()(0)), hookhandler) If hookhandle <> 0 Then MsgBox("hi") End Sub Shared Function ChangeButtonHost(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) If nCode >= 0 Then Dim cwps As CWPSTRUCT = DirectCast(System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, GetType(CWPSTRUCT)), CWPSTRUCT) Dim winfo As New WindowInfo(cwps.hwnd) If cwps.message = &H46 And WindowInfo.IsValid(cwps.hwnd) And winfo.ShowInTaskbar = True Then RaiseEvent WindowMoving(cwps.hwnd) End If End If Return 0 'User32Imports.CallNextHookEx(hookhandle, nCode, wParam, lParam) End Function End Class
Friend Class User32Imports
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookHandler, ByVal hInstance As IntPtr, ByVal ThreadId As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lparam As IntPtr) As Integer
End FunctionEnd Class>
EDIT: CreateSystemHook was my attempt at doing the
Maybe this CodeProject article will help you: http://www.codeproject.com/KB/cs/globalhook.aspx
-
Maybe this CodeProject article will help you: http://www.codeproject.com/KB/cs/globalhook.aspx
Thats for the mouse and keyboard hooks right? Neither will work in this case.