I got the following signature from
the E appendix of Adam Nathan's
".NET and COM...Interoperability Guide".
---------------------------------------------------------
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx
(
int idHook,
HookDelegate lpfn,
IntPtr hMod,
uint dwThreadId
);
------------------------------------------------------------
For the most part Nathan's book is over my head.
But I did get it from it not to get too worried
over the superficial differences in the signature
for this that I've seen, in different tutorials on the net.
However, Nathan's generic "HookDelegate"
has to be replaced with something specific.
And I want to hook both, the keyboard,
and the mouse (-via a single declaration
of SetWindowsHookEx().)
So I need a delegate signature that
works for both the keyboard and
the mouse.
Whereas the delegates for these
that I've seen are pretty obviously
incompatible:
--------------------------------------------
delegate int KeyboardDelegate
(
int code,
int wparam,
int lparam
);
delegate int MouseDelegate
(
int code,
int wparam,
ref MOUSEHOOKSTRUCTEX lparam
);
(MOUSEHOOKSTRUCTEX
being the appropriately defined
structure. )
---------------------------------------------
( Replacing "ref MOUSEHOOKSTRUCTEX"
with "int" and then trying to cast "lparam"
to "MOUSEHOOKSTRUCTEX"
-- doesn't work. )
So, anyway, thank you, anyone,
who understands what I'm asking.
(I'm quite new to all this.
So please forgive me if it's a FAQ
or anything like that.)
~Greg.
Mattias Sj?gren - 19 Apr 2004 08:24 GMT
>And I want to hook both, the keyboard,
>and the mouse (-via a single declaration
>of SetWindowsHookEx().)
Why does it have to be a single declaration?
>( Replacing "ref MOUSEHOOKSTRUCTEX"
>with "int" and then trying to cast "lparam"
>to "MOUSEHOOKSTRUCTEX"
>-- doesn't work. )
If you make it an IntPtr you can then use Marshal.PtrToStructure to
dereference the pointer to a MOUSEHOOKSTRUCTEX struct.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
~greg - 19 Apr 2004 14:31 GMT
"Mattias Sj?gren" > ..
> >And I want to hook both, the keyboard,
> >and the mouse (-via a single declaration
> >of SetWindowsHookEx().)
>
> Why does it have to be a single declaration?
To be honest, I didn't know about "EntryPoint".
However, now that I've got them working together,
I find that the keyboard and the mouse can share
one "CallNextHookEx()" and one
"UnhookWindowsHookEx()", but not
one "SetWindowsHookEx()".
I understand that there have to be separate
delegates for the two callbacks.
I was just hoping they could share
one "SetWindowsHookEx()", --just
to reduce the noise level, --since I've
got all the signatures identical now,
--thanks to your "Marsh.PtrToStructure"
clue.
Anyway, the clue is what got me on the
right path, --in the sense that the hooks
work. So thank you!
~Greg.
> >( Replacing "ref MOUSEHOOKSTRUCTEX"
> >with "int" and then trying to cast "lparam"
[quoted text clipped - 5 lines]
>
> Mattias