C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-19-2003, 01:14 AM   #1
Registered User
 
Join Date: Jul 2002
Posts: 3
Question How to keep static data in a keyboard hook?

I created a simple DLL to do a global keyboard hook, but I was unable to communicate data between the procedure which set up the hook and the hook itself:



HHOOK hKeyboardHook = NULL;
int i = 0;


LRESULT CALLBACK KeyboardHookProc(int code, WPARAM wParam, LPARAM lParam) {
if (code < 0)
return CallNextHookEx(hKeyboardHook, code, wParam, lParam);

if (i = 0) {
MessageBox(NULL, "ERROR, DATA NOT COMMUNICATED", "ERROR", 0);
}

return 0;
}

extern "C" HHOOK CFHookKeyboard(void) {
if (!hKeyboardHook)
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProc, (HINSTANCE)hMod, 0);

i = 1;

return hKeyboardHook;
}



Anyone knows of any fix?
junbin is offline   Reply With Quote
Old 01-19-2003, 03:24 AM   #2
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,183
The problem you have is that each dll will have a seperate copy of hKeyboardHook......in the process of the app that creates the hook, it will be valid, but in all other instances if will be void........

Many compilers offer a global memory option for dlls - kind of like a static variable in a c++ class - you can have as many dlls running in the system as you need, but they all refer to one area of system memory.....in that shared area, you place the HHOOK

For VC++ you use;

Code:
#pragma data_seg(".syshook") //Set shareable segment
HHOOK hKeyboardHook = NULL; //Handle to my hook
#pragma data_seg()
#pragma comment(linker, "/section:.syshook,rws")
Now every dll uses the same HHOOK......

Also add to this that the hook procedure (when called) exists in another process.........that means all shared handles (file...etc) are unusable (and you cant share them accross process boundries as you dont have a list of the processes that need them)...they way I get around this is for the launching app to open a MailSlot (CreateMailSlot()) and then create a thread to recieve messages......then I use CreateFile() in the hook dll to get a handle to the Mailslot (which is usable in my process) and write output to it.......
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simulate Keys with a Keyboard Hook guitarist809 Windows Programming 3 11-14-2008 08:14 PM
Code review Elysia C++ Programming 71 05-13-2008 09:42 PM
get keyboard and mouse events ratte Linux Programming 10 11-17-2007 05:42 PM
Linking errors with static var Elysia C++ Programming 8 10-27-2007 05:24 PM
Warnings, warnings, warnings? spentdome C Programming 25 05-27-2002 06:49 PM


All times are GMT -6. The time now is 10:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22