Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Windows hook

Windows hook

Scheduled Pinned Locked Moved Visual Basic
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robertw019
    wrote on last edited by
    #1

    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 Function

    End Class>
    

    EDIT: CreateSystemHook was my attempt at doing the

    D S 2 Replies Last reply
    0
    • R robertw019

      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 Function

      End Class>
      

      EDIT: CreateSystemHook was my attempt at doing the

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        R Offline
        R Offline
        robertw019
        wrote on last edited by
        #3

        Thanks for the response. I've looked at the MSDN docs for hooks, and created a C++ version, but I'm getting the same results.

        1 Reply Last reply
        0
        • R robertw019

          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 Function

          End Class>
          

          EDIT: CreateSystemHook was my attempt at doing the

          S Offline
          S Offline
          scofflaw
          wrote on last edited by
          #4

          Maybe this CodeProject article will help you: http://www.codeproject.com/KB/cs/globalhook.aspx

          R 1 Reply Last reply
          0
          • S scofflaw

            Maybe this CodeProject article will help you: http://www.codeproject.com/KB/cs/globalhook.aspx

            R Offline
            R Offline
            robertw019
            wrote on last edited by
            #5

            Thats for the mouse and keyboard hooks right? Neither will work in this case.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups