Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a global hook

Status
Not open for further replies.

stef88

MIS
Apr 27, 2007
13
GB
I'm trying to install a global hook. If i issue this command :
hHook = SetWindowsHookEx(WH_KEYBOARD, MouseProc, 0,GetCurrentThreadId());
it works fine, but, of course, this isn't a global hook, it's only a local one. Now, this should be a global hook:
hHook = SetWindowsHookEx(WH_KEYBOARD, MouseProc, 0,0);
but it doesn't seem to work at all.
I'm using Visual C++ 6.0 .
Any ideas anyone, please?

PS I have a problem with the local hook too. It executes twice, i suppose that once is for keydown, and once for keyup, but i don't know how to differentiate these.
 
Why MouseProc for keyboard hook?

Try to check bit 30 of lParam (3rd arg of keyboard hook procedure - Previous Key-State Flag). It set to 1 for WM_KEYDOWN and 0 for WM_KEYUP.
See MSDN for more info...
 
Well... I slipped that mistake, but the name doesn't matter. The function is ok. I tried to read the flags but i don't know how. If i transform the flags to string (with itoa()) and then display them in a messagebox i get a large number. Strange is that the last digit isn't 0 or 1 (as it should have been), but 3 or other digits... Any more ideas please?
 
Ideas? To select a bit in a word, as usually in C and C++:
Code:
if ((dword & 0x40000000) != 0) // mask for bit # 30...
...
The 3rd parameter of callback KeyboardProc function is LPARAM type dword...
 
Thanks for the example, i forgot about byte operators...
Now, any ideas linked to the global hook?
 
Global hook callback procedure parameter must point to a procedure in a DLL, not in the code associated with the current process (see MSDN for SetWindowHookEx description).
Is it condition OK in your case?
 
I succeded. Thank you!

HMODULE handledll;
handledll=GetModuleHandle("hookdll");
hHook = SetWindowsHookEx(WH_KEYBOARD, MouseProc, handledll,0);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top