October 24, 2004
Greetings,

I need the help of someone with a grasp on windows system hooks and DLL's.
I have created a DLL containing a keyboard hook procedure. In another driver application I am loading the library, getting the procedure address and successfully calling SetWindowsHookEx to set up a global keyboard hook. My procedure then is intercepting all keystrokes throughout the system as expected.
So far, so good.
Since I want also to be able to call any other chained hook procedures, I also added an init function to the dll that gets the registered hook handle and stores it so that it can be passed to CallNextHookEx. The stored hook handle needs to be shared among all instances of the DLL, so I made the DLL's data segment shared. After making the data segment shared, however, my hook stopped working properly. It would intercept keystrokes in some applications only, while others would go unintercepted. Which applications are intercepted seems to vary. I have also noticed that sometimes applications would crash after having installed that hook.
What problems could cause that shared data segment and could they be avoided without resorting to other methods of sharing data between DLL instances?

Any comments and suggestions appreciated.

Best regards,
Dimitri
October 24, 2004
Dimitri Kaparis wrote:
> Greetings,
> 
> I need the help of someone with a grasp on windows system hooks and DLL's.
> I have created a DLL containing a keyboard hook procedure. In another driver application I am loading the library, getting the procedure address and successfully calling SetWindowsHookEx to set up a global keyboard hook. My procedure then is intercepting all keystrokes throughout the system as expected.
> So far, so good.
> Since I want also to be able to call any other chained hook procedures, I also added an init function to the dll that gets the registered hook handle and stores it so that it can be passed to CallNextHookEx. The stored hook handle needs to be shared among all instances of the DLL, so I made the DLL's data segment shared. After making the data segment shared, however, my hook stopped working properly. It would intercept keystrokes in some applications only, while others would go unintercepted. Which applications are intercepted seems to vary. I have also noticed that sometimes applications would crash after having installed that hook.

A small update to my problem:
I tried disabling data segment sharing and instead relocating the hook handle variable in a separate source file and running
patchobj data.obj -l_DATA _SHARED
on it. The procedure intercepts all keystrokes properly now, but data seems not to be shared.
In the main dll source file I'm declaring the variable as follows:

extern HHOOK KeyHook;

It is defined in data.c, which is converted to shared before linking.
The init function assigns to it the passed value, but only the calling program's instance is updated - all other remain at their initial value.
I also noticed that any instances of the Command Prompt (cmd.exe) are also updated.

What am I doing wrong?

Regards,
Dimitri