|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Win32 Hooks
For educational purposes/fun I've been messing around with some Windows work. I'm working with hooks (a keyboard hook to be specific), and I want it to work on a global level. According to this MSDN article the hook procedure has to be in a seperate dll. I've done this, and then called SetWindowsHookEx() as instructed here. idHook is set to WH_KEYBOARD, lpfn is set to the pointer to the hook procedure (loaded from the dll using LoadLibrary() and GetProcAddress() ), hMod is the handle to the dll, and dwThreadId is set to 0 (as is required to make the hook global according to the site). However, when I run it (and I've confirmed this using breakpoints), the hook only catches keyboard messages to the window that launched it. It works only on the thread level, and not globally. Any help would be greatly appreciated, as I don't know where I've went wrong. Everything else works perfectly. Attached is the code.
|
|
#2
|
|||||
|
|||||
|
Re: Win32 Hooks
It's been a couple years since I've even looked at Win32 code, so I'm not sure how much help I can be, but...
Any reason why you are torturing yourself and not using .NET? Or at least MFC? |
|
#3
|
||||
|
||||
|
Re: Win32 Hooks
I started in .NET and did a lot with it back in the day. I did some freeware apps online and a lot of people complained about the .NET framework. I wanted to learn native a) for the fun of it, and b) so I could build faster programs.
|
|
#4
|
||||
|
||||
|
Re: Win32 Hooks
I've only done a little with low level hooks, and mostly in C# (see attached). The most blatant thing I noticed when comparing my code to yours is I'm using WH_KEYBOARD_LL and you're using WH_KEYBOARD. Does the behavior change if you use WH_KEYBOARD_LL? (I assume you're not using Win95 or something so you might as well use WH_KEYBOARD_LL)
--Ryan Last edited by RyanCahoon : 24-04-2009 at 22:39. |
|
#5
|
||||
|
||||
|
Re: Win32 Hooks
I'm running Vista. Anyways, WH_KEYBOARD_LL apparently catches messages when "a new keyboard input event is about to be posted into a thread input queue" and WH_KEYBOARD catches messages "whenever an application calls the GetMessage or PeekMessage function and there is a keyboard message (WM_KEYUP or WM_KEYDOWN) to be processed". For my purposes, I thought WH_KEYBOARD would be fine. And according to the MSDN explanation of SetWindowsHookEx(), WH_KEYBOARD should be able to run globally.
EDIT: Using WH_KEYBOARD_LL, the hook works globally. However I cannot do what I wanted to (which is change the keystroke en-route to programs). WH_KEYBOARD_LL passes a pointer to a struct containing they key data (such as key code, etc.) as it's lParam, but i have found that changing the data in the struct does not affect the output (even If I change the keycode the key remains the same). Can't I change the data in the message using the hook? Last edited by slavik262 : 25-04-2009 at 02:40. |
|
#6
|
||||
|
||||
|
Re: Win32 Hooks
Quote:
Let me know if that works. --Ryan P.S. I'm not sure why WH_KEYBOARD_LL works and WH_KEYBOARD doesn't. I think I remember running into the same issue with mine. Maybe posting to the MSDN forums would shed a bit more light on the subject. |
|
#7
|
||||
|
||||
|
Re: Win32 Hooks
Quote:
Last edited by slavik262 : 29-04-2009 at 19:38. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Special grappling hooks for throwers? | ShadowNinja | General Forum | 3 | 04-01-2009 17:48 |
| win32 dialog help | Chris Bright | Programming | 10 | 30-12-2004 00:17 |
| Win32 serial port - recieve buffer | seanwitte | Programming | 4 | 05-11-2003 21:41 |
| Win32 API Port Connection Help | Raven_Writer | Programming | 3 | 17-01-2003 14:23 |
| Win32 Dialog Help | Raven_Writer | Programming | 2 | 12-01-2003 09:37 |