Jump to content
  • Advertisement
Sign in to follow this  
jchmack

Problems with Windows hooks

This topic is 2590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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" <<endl;
}

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!