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. C#
  4. Hooks CallWndProc to get my mouse

Hooks CallWndProc to get my mouse

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 3 Posters 3 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.
  • C Offline
    C Offline
    c121hains
    wrote on last edited by
    #1

    I noticed that my HOOK function isn't capturing mouse related messages. But others like WM_ACTIVEAPP is fine. I'm using the CallWndProc method instead of a mouse hook procedure. I want to be able to peek at ALL mouse and keyboard related windows messages across the desktop and other applications. The mouse hook seemed to only capture what was being delivered to my app. When i was experimenting with the mouse hook proc, i did this: hHook = SetWindowsHookEx(WH_MOUSE,MouseHookProcedure, (IntPtr)0,0); .. to specify all threads across the desktop (last param). But it crashed here. Doesn't like the last two parameters 0 and 0. So this is why i'm not trying CallWndProc. Any ideas of how to do this? private void button8_Click(object sender, System.EventArgs e) { if(hHook == 0) { CWPHookProcedure = new HookProcDT(Form1.CWPHookProc); hHook = SetWindowsHookEx (WH_CALLWNDPROC, CWPHookProcedure, IntPtr.Zero, User32.GetWindowThreadProcessId(this.Handle.ToInt32(), 0)); .. etc .. and then in my CallWndProc hook method: public static int CWPHookProc(int code, IntPtr wparam, IntPtr lParam) { if (code < 0) return User32.CallNextHookEx(hHook, code, wparam, lParam); CWPSTRUCT cwp = (CWPSTRUCT) Marshal.PtrToStructure( lParam, typeof(CWPSTRUCT)); if (code == 0) //this means that the hook procedure should process the //message contained in CWPSTRUCT { switch(cwp.message) { case WM_ACTIVATEAPP: //GET'S HERE MessageBox.Show("Is here in CWPHookProc"); break; case WM_MOUSEMOVE: //BUT NOT HERE MessageBox.Show("Is here in CWPHookProc"); break; } } return User32.CallNextHookEx(hHook, code, wparam, lParam); } //Declare wrapper managed CWPStruct class. [StructLayout(LayoutKind.Sequential)] public class CWPSTRUCT { public IntPtr lparam; public IntPtr wparam; public int message; public IntPtr hwnd; }

    D R 2 Replies Last reply
    0
    • C c121hains

      I noticed that my HOOK function isn't capturing mouse related messages. But others like WM_ACTIVEAPP is fine. I'm using the CallWndProc method instead of a mouse hook procedure. I want to be able to peek at ALL mouse and keyboard related windows messages across the desktop and other applications. The mouse hook seemed to only capture what was being delivered to my app. When i was experimenting with the mouse hook proc, i did this: hHook = SetWindowsHookEx(WH_MOUSE,MouseHookProcedure, (IntPtr)0,0); .. to specify all threads across the desktop (last param). But it crashed here. Doesn't like the last two parameters 0 and 0. So this is why i'm not trying CallWndProc. Any ideas of how to do this? private void button8_Click(object sender, System.EventArgs e) { if(hHook == 0) { CWPHookProcedure = new HookProcDT(Form1.CWPHookProc); hHook = SetWindowsHookEx (WH_CALLWNDPROC, CWPHookProcedure, IntPtr.Zero, User32.GetWindowThreadProcessId(this.Handle.ToInt32(), 0)); .. etc .. and then in my CallWndProc hook method: public static int CWPHookProc(int code, IntPtr wparam, IntPtr lParam) { if (code < 0) return User32.CallNextHookEx(hHook, code, wparam, lParam); CWPSTRUCT cwp = (CWPSTRUCT) Marshal.PtrToStructure( lParam, typeof(CWPSTRUCT)); if (code == 0) //this means that the hook procedure should process the //message contained in CWPSTRUCT { switch(cwp.message) { case WM_ACTIVATEAPP: //GET'S HERE MessageBox.Show("Is here in CWPHookProc"); break; case WM_MOUSEMOVE: //BUT NOT HERE MessageBox.Show("Is here in CWPHookProc"); break; } } return User32.CallNextHookEx(hHook, code, wparam, lParam); } //Declare wrapper managed CWPStruct class. [StructLayout(LayoutKind.Sequential)] public class CWPSTRUCT { public IntPtr lparam; public IntPtr wparam; public int message; public IntPtr hwnd; }

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #2

      c121hains wrote: I want to be able to peek at ALL mouse and keyboard related windows messages across the desktop and other applications. What you need are global hooks. Fortunately there are WH_MOUSE_LL[^] and WH_KEYBOARD_LL[^] which don't use code injection and can be used with managed code. Check out Processing Global Mouse and Keyboard Hooks in C#[^] to get a working code example. Best regards Dennis

      C 1 Reply Last reply
      0
      • D Dennis C Dietrich

        c121hains wrote: I want to be able to peek at ALL mouse and keyboard related windows messages across the desktop and other applications. What you need are global hooks. Fortunately there are WH_MOUSE_LL[^] and WH_KEYBOARD_LL[^] which don't use code injection and can be used with managed code. Check out Processing Global Mouse and Keyboard Hooks in C#[^] to get a working code example. Best regards Dennis

        C Offline
        C Offline
        c121hains
        wrote on last edited by
        #3

        This worked great! Thank you!

        1 Reply Last reply
        0
        • C c121hains

          I noticed that my HOOK function isn't capturing mouse related messages. But others like WM_ACTIVEAPP is fine. I'm using the CallWndProc method instead of a mouse hook procedure. I want to be able to peek at ALL mouse and keyboard related windows messages across the desktop and other applications. The mouse hook seemed to only capture what was being delivered to my app. When i was experimenting with the mouse hook proc, i did this: hHook = SetWindowsHookEx(WH_MOUSE,MouseHookProcedure, (IntPtr)0,0); .. to specify all threads across the desktop (last param). But it crashed here. Doesn't like the last two parameters 0 and 0. So this is why i'm not trying CallWndProc. Any ideas of how to do this? private void button8_Click(object sender, System.EventArgs e) { if(hHook == 0) { CWPHookProcedure = new HookProcDT(Form1.CWPHookProc); hHook = SetWindowsHookEx (WH_CALLWNDPROC, CWPHookProcedure, IntPtr.Zero, User32.GetWindowThreadProcessId(this.Handle.ToInt32(), 0)); .. etc .. and then in my CallWndProc hook method: public static int CWPHookProc(int code, IntPtr wparam, IntPtr lParam) { if (code < 0) return User32.CallNextHookEx(hHook, code, wparam, lParam); CWPSTRUCT cwp = (CWPSTRUCT) Marshal.PtrToStructure( lParam, typeof(CWPSTRUCT)); if (code == 0) //this means that the hook procedure should process the //message contained in CWPSTRUCT { switch(cwp.message) { case WM_ACTIVATEAPP: //GET'S HERE MessageBox.Show("Is here in CWPHookProc"); break; case WM_MOUSEMOVE: //BUT NOT HERE MessageBox.Show("Is here in CWPHookProc"); break; } } return User32.CallNextHookEx(hHook, code, wparam, lParam); } //Declare wrapper managed CWPStruct class. [StructLayout(LayoutKind.Sequential)] public class CWPSTRUCT { public IntPtr lparam; public IntPtr wparam; public int message; public IntPtr hwnd; }

          R Offline
          R Offline
          Richard Schneider 0
          wrote on last edited by
          #4

          I've added a MouseController to http://nunitforms.sourceforge.net/news.html[^]. This code is well documented (I believe) with NUnit test case. Cheers, Richard Schneider

          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