Can't set windows hook - what wrong with my code ?
Hi All,
I need to catch all keyboard event and some of the keyboard keys i need to intercept.
So i using windows hook to do it.
But in some reason when i do "SetWindowsHookEx" i getting wrong return code and my hook does not working.
This is my code ( just the relevant part ):
LRESULT CALLBACK KeyboardProc(int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
LRESULT lRetCode = 0;
return lRetCode;
}
LRESULT SetHook()
{
HINSTANCE hinstance = NULL;
HHOOK hookHandle = NULL;
LRESULT lRetCode = 0;
hookHandle = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hinstance, 0);
return lRetCode;
}
What i did wronge ?
Thanks for any help.
Re: Can't set windows hook - what wrong with my code ?
void main(void)
{
HMODULE hModule= LoadLibrar("yourhook.dll");
fKeyProc fp = (fKeyProc) GetProcAddress(hModule, "KeyProc");
SetWindowsHookEx(WH_KEYBOARD, fp, (HINSTANCE)hModule, 0);
}
Hope this helps.
Re: Can't set windows hook - what wrong with my code ?
Re: Can't set windows hook - what wrong with my code ?
Yes, the procedure should be in a dll. Then only, can OS map it to all the processes
Re: Can't set windows hook - what wrong with my code ?
Quote:
Originally Posted by YANSHOF
This is my code ( just the relevant part ):
You missed most relevant part: hook callback.
Global hook in a DLL needs shared data segment. It is most likely that you did not use it.
If you are going to use your hook only in NT family you can use low level keyboard hook that is always global and can be set with callback in n exe module.
Re: Can't set windows hook - what wrong with my code ?
You should make the HOOK handle globaly shared in DLL's shared segment,
How To Share Data Between Different Mappings of a DLL