Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

What delegate works for BOTH keyboard AND mouse?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
~greg - 17 Apr 2004 21:31 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.