www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - System hooks & DLL shared segments

↑ ↓ ← Dimitri Kaparis <d_kaparis universalstudyhelper.com> writes:
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
Oct 24 2004
↑ ↓ → Dimitri Kaparis <d_kaparis universalstudyhelper.com> writes:
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
Oct 24 2004