Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with Windows hooks

Started by jchmack Nov 11, 2011 at 8:20 AM 0 replies 1.2k views
Original Post
jchmack
jchmack
I am trying to read Windows Messages from another window. I have gotten this code to work with notepad:



HINSTANCE HookhInstance = NULL;
DWORD ThreadId = 0;
DWORD ProcessId = 0;


HookhInstance = GetModuleHandle( __TEXT("user32") );
ThreadId = GetWindowThreadProcessId(InputWindow, &ProcessId);
hkb = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)MyHookProc,HookhInstance,ThreadId);

if(hkb != NULL)
{

char title[80];

GetWindowTextA(InputWindow,title,sizeof(title));
string titlestring(title);
cout << "hooked " << titlestring << endl;
}



and my hook proc:



LRESULT CALLBACK MyHookProc(int intCode, WPARAM wParam, LPARAM lParam)
{
cout << "HookProc" <<endl;

}



The code works fine when reading from notepad. I get the "hooked" text in my program and it says the correct name of the window. When I use the keyboard in notepad i get the "HookProc" text in my program.

The problem is that when I try to use this same program to target something other than notepad. I get the dialog that says I hooked it properly so I believe I am hooking the right window, but none of the keyboard messages are making it back to my program.

I am trying to follow the logic of spy++:
http://blogs.msdn.co...-internals.aspx

I have used a program to make sure that the program I am trying to read messages from is using user32 since I am placing my hook there:
http://www.dependencywalker.com/

I believe it has to be possible because spy++ is somehow able to get the mouse/key info i would like to have. I have tinkered around with this for a while and I am starting to get stumped. Any help would be greatly appreciated.

Ty for any help in advance =).


edit: sorry about the weird spacing in code brackets i just cant get it to look right lol.
Vortez
Vortez
That's probably because you forgot to call CallNextHook as a return value. Also, msdn say that if code is less than 0, you must call CallNextHook() without doing anything, so your function should look like this:

LRESULT CALLBACK MyHookProc(int intCode, WPARAM wParam, LPARAM lParam)
{
if(intCode >= 0){
cout << "HookProc" < }

return CallNextHook(hkb, intCode, wParam, lParam);
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.