Link to home
Start Free TrialLog in
Avatar of d-fader
d-fader

asked on

Global mouse hook

Hey people,

I've got a real strange problem... I've made a DLL with a key and mouse
hook. Besides that I've made a simple application with an OnCreate event
handler, in which the DLL is loaded and the key and mouse hooks are set (by
calling a method in the DLL which hooks the hooks) and an OnClose event
handler in which I unhook the hooks and free the library again.
No problems whatsoever, everything works flawless! ...until I start to play
a little with Internet Explorer...
When I start the application and open some (20 or so) Internet Explorer
windows and then close my application, one of the IEs crashes with a strange
error message and an runtime error 217. Little after that (when I use one of
the IE windows again) they ALL crash. This is probably because they all are
invoked by eachother.

Ofcourse my question is: WHAT is the problem? I've isolated the problem in
the 'UnhookWindowsHookEx( hwndMouseHook );', at that time the key hook is
already freed. Ofcourse the hwndMouseHook variable is not 0.

I'm doing everything 'by the book', the "MouseProc" is set up exactly as
MicroSoft descibes it.
Also I've build in a piece of code that disables recursion in the MouseProc.
I've done this by keeping track of a counter. This counter is maintained by
a Mapping I made, so it's exchangable by all processes running.

I hope someone can help me with this problem and I hope my explanation is
detailed enough :)

Thanks!
d-fader
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

hello d-fader, since you seem to be new at creating DLL hooks, there may be things that you did not account for in your code, since you do not show any code, I can not tell what you have done. You moght look at this web page about Hooks -

http://delphi.about.com/library/bluc/text/uc063001a.htm


there are many, many pages hee at EE about Hooks, you might look at this one for a keyboard hook -

https://www.experts-exchange.com/questions/20347433/Translate-Key-Code-message-lparam.html

ask questions if you need more information.
Avatar of d-fader
d-fader

ASKER

Well, I'm indeed quite new to hooks, at least, on the win32 platform :)
Here's the code of my MouseProc (the actual hook). The PSettings 'record' is the shared memory mapping.
As far as I know, there isn't anything wrong with this code. Maybe I could change the SendMessage to a Postmessage or a SendMessageTimeout, but that just doesn't help.

What I don't understand is why the crash happens on the Unhook statement. I've figured out something strange as well. The computer where I'm at now, seems to not have the problem I have! I haven't tested this thouroughly, but could it be that something else (another process) is interfering with my own keyhook?

Anyway, as far as I know, I'm doing everything by the book.

Here's the code of the mousehook proc. The 'strange' if statement filters out the mouse buttons (left, right, double-left, double-right).

I hope you can find something terribly wrong in this code, cause then I can solve the problem :P

Thanks!


function MouseProc( nCode: Integer; msgID: WPARAM; msgMouseCoords: LPARAM ): LRESULT; stdcall;
begin

 if ( nCode = hc_Action ) and ( PSettings.FInMouseProc = 0 ) then
   try
     Inc( PSettings.FInMouseProc );  {Disables Recursion's}

     if ( msgID and 7 = 1 ) or (msgID and 7 = 3) or (msgID and 7 = 6) or (msgID and 7 = 4) then
       SendMessage( PSettings.AppHandle, WM_USERACTIVITY, 0, 0 );

   finally
     Dec( PSettings.FInMouseProc );
   end;

 Result := CallNextHookEx( hwndMouseHook, nCode, msgID, msgMouseCoords );
end;
 
first of all. .  Why would you use SendMessage in

SendMessage( PSettings.AppHandle, WM_USERACTIVITY, 0, 0 );

if you DO NOT USE the result integer? ?, unless you need the Result integer (or PostMessage won't work)  then use PostMessage. But This may not solve your problem. .  I do not see a need to Disables Recursion's, , althouh there may be code that would require something like that, why did you do that? , , , , ,  Can you show the code for UnHooking the keyboard and mouse? You seem to think that the problem happens there.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
d-fader, do you need more help with this? ?
Is this question still active? ?
d-fader:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.