views:

86

answers:

2

Hi,

I have an application with both .NET and Delphi components I register to a keyboard hook (with SetWindowsHookEx) in both components. I first register in .NET, and later in Delphi.

The problem is, the hook delegate in Delphi is called before the hook delegate in .NET.

According to MSDN, the hook chain is just a list, and as I understand the delegates should be called according to the order of registration.

Anyone has an idea what is going on here? Thanks in advance!

+1  A: 

The hook list is not a list, but a chain. Newly installed hook keeps the reference to the previous one. This means that the hook, installed later, is always executed before the hook installed earlier.

Eugene Mayevski 'EldoS Corp
+2  A: 

You've misunderstood. The hook overview in MSDN describes it like this (emphasis added):

The SetWindowsHookEx function always installs a hook procedure at the beginning of a hook chain. When an event occurs that is monitored by a particular type of hook, the system calls the procedure at the beginning of the hook chain associated with the hook. Each hook procedure in the chain determines whether to pass the event to the next procedure. A hook procedure passes an event to the next procedure by calling the CallNextHookEx function.

Therefore, it's precisely the expected behavior if your Delphi hook is installed last and is called first. There's nothing "going on" at all.

Rob Kennedy
Thanks! I don't know how I missed that, need to sleep more.
Tomer O.