Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C# DLL
Hi!
 
Sorry If I posted this to the wrong place.
 
I would like to catch user32.dll GetKeyState and similar function calls of an already running process.
I am not so good in hooking so tried to dig up something usable library (source code) that works on all Windows version from Windows XP: XP, Vista, Windows 7 etc. (both of x86 and x64!!!).
I have not found any. Could anyone link a working solution for my problem?
 
Thank you!
Posted 27-Apr-11 9:54am
Comments
P1l19r1m at 27-Apr-11 14:59pm
   
Are you interested in keyboard hook?
EURaklap at 27-Apr-11 15:05pm
   
I am writing a so called "helper application" for a game and this game using GetKeyState to read states of modifier keys, so I think the only thing what I can do if I want to send CTRL+O to this game that I hook these functions. :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This one works for pre-Windows 7 and is very popular:
 
API hooking revealed[^]
 
And this one claims to work for Windows 7 too (I have not heard of it until today though, so evaluate it and then decide):
 
http://easyhook.codeplex.com/[^]
  Permalink  
Comments
EURaklap at 27-Apr-11 15:24pm
   
Already tried each without success. API hooking revealed not works W7 (x64), neither EasyHook. :(
Nishant Sivakumar at 27-Apr-11 15:26pm
   
Yeah, it would seem that there are no public libraries yet (none that are popular anyway). There are apps that do it but their authors have not revealed their methods :-)
Espen Harlinn at 27-Apr-11 15:40pm
   
Nice links, my 5
Manfred R. Bihy at 27-Apr-11 16:20pm
   
I was going to say that!
Manfred R. Bihy at 27-Apr-11 16:20pm
   
Nice information! 5+
Nishant Sivakumar at 27-Apr-11 16:21pm
   
Thanks :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If nothing else seems to do the trick, you can always consider using:
Windows Driver Kit
[^]
Specifically:
Kbfiltr - WDF Version
[^]
KbdClass[^]
 
Regards
Espen Harlinn
  Permalink  
Comments
Nishant Sivakumar at 27-Apr-11 15:41pm
   
A good lower level approach, but sometimes that's just what's needed. My vote of 5.
Espen Harlinn at 27-Apr-11 15:49pm
   
Thanks Nishant :)
Manfred R. Bihy at 27-Apr-11 16:19pm
   
Good answer! 5+
Espen Harlinn at 27-Apr-11 17:25pm
   
Thanks Manfred :)
EURaklap at 27-Apr-11 16:28pm
   
Ouch, writing a driver is too specific, professional and unneccesary I think, but very good idea :)
Espen Harlinn at 27-Apr-11 17:30pm
   
As I mentioned "if nothing else seems to do the trick", this is an option of last resort if you haven't done anything like this before. On the other hand, if you've done a filter driver before, it actually fairly straight forward as the driver kit comes with sample drivers :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

It's not free but probably the best API hook system is "madCodeHook".
http://www.madshi.net/madCodeHookDescription.htm
 
Best because it's very robust and stable.
Supports all 32 and 64 bit Windows operating systems from Windows 95 to Windows 7, etc.
Note, I make no money out of this, I'm just a paid user of it.
I've made my own little hooking engines, used others, etc., and madCHook is the best.
  Permalink  
Comments
EURaklap at 21-May-11 4:59am
   
I know this very great lib, but it costs too much for only one free project.:(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

  Permalink  
Comments
EURaklap at 21-May-11 4:59am
   
Yes, It does not work on W7 x64 :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hello!!!
 
I saw your question.
 
Your problem can be solved by several ways.
 
1. Hooking Keyboard in Kernel land.
2. Hooking Keystate in User land by using SetWindowsHookex() function.
3. I think that in your case, you can use a keybd_event() function to send keypress sign to your game.
 
Which is your case?
 
Regards
  Permalink  
Comments
EURaklap at 28-Apr-11 5:09am
   
Neither :)
My app should work in background without disturbing the user, so user can do anything what he wants. I would like to modify keystate in a particular running process.
xnli at 28-Apr-11 5:42am
   
Yeah. It can be possible enough and be solved by two ways.
In user land, you can use SetWindowsHookex() function and you can change wParam and lParam for your purpose.
then call CallNextHookex() function.
In another way, you can hook Keyboard Driver and change Parameter.
If you need, I will post an article.
How about?
EURaklap at 28-Apr-11 8:40am
   
I would be very happy If you share with me some links where I found a working solution for my problem: modify keystates of another thread on x86 and x64 windows (XP and above) while my application nor the modified application is not actived/focused. I did not find any working solution. (Searching 5 days. lol)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can start using this code snippet. I've used it in my practice.
 
#include <stdio.h>
#include <windows.h>
 
void CheckKey(int key);
 
LRESULT CALLBACK KeyboardHook(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);
 
typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode; // virtual key code
DWORD scanCode; // scan code
DWORD flags; // flags
DWORD time; // time stamp for this message
DWORD dwExtraInfo; // extra info from the driver or keybd_event
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
 
HHOOK hHook;
 
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
hHook = SetWindowsHookEx(13, KeyboardHook, hInstance , 0);
while (GetMessage(NULL,NULL,0,0)) ; // NOP while not WM_QUIT
return UnhookWindowsHookEx(hHook);
}
 
LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode == HC_ACTION)
if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN)
CheckKey (((PKBDLLHOOKSTRUCT)lParam)->vkCode);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
 
void CheckKey(int key)
{
if (key==VK_CONTROL)
// do what you need to do at "CTRL" button clickedSmile | :)
}
  Permalink  
Comments
EURaklap at 27-Apr-11 16:25pm
   
Main problem is how to injecting and hooking an already running process on all windows versions including x64 and the mentioned application using GetKeyState function to read states of modifier keys and my application should work in the background.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 21 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100