473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Global Mouse Hooks in C#

Hi

Please help because I'm really stuck:

I'm trying to write an application in C# which can be used to track
the mouse pointer position on any window. I'm trying to use a Global
mouse hook to do this but with limited success. According to the
documentation (as I understand it) you can only do Global hooks in
..Net if the delegate procedure passed to SetWindowsHookEx is external.

To get around this, I have written a function in C++ and declared it
for use in my C# program as follows:

[DllImport("MouseCallBack.dll",CharSet=CharSet.Auto ,
CallingConvention=CallingConvention.StdCall)]
public static extern int MouseHookProc(int nCode, IntPtr wParam,
IntPtr lParam);

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

HookProc MouseHookProcedure = new HookProc(MouseHookProc);

hHook = SetWindowsHookEx(WH_MOUSE,MouseHookProcedure,
(IntPtr)0,
AppDomain.GetCurrentThreadId());

However, when I try to set the last parameter (ThreadID) of
SetWindowsHookEx to zero to make the hook Global, the hook fails. Am I
doing something wrong or can you just not do Global hooks in .Net at
all? If not, does anyone have any ideas of how to achieve my goal
without the use of global hooks?

Also, if it can be done, is it possible, using some sort of event
handler for my external "MouseHookProc" in "MouseCallBack.dll" to
callback to the C# program and notify it of the new mouse position?

Thanks in advance. Paul.
Jul 19 '05 #1
3 12554
Paul,
Am I
doing something wrong or can you just not do Global hooks in .Net at
all?


Can't be done. If you need to use a global hook, implement it in
native code library instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 19 '05 #2
Paul,

Here's an example of a global hook used with a VB app.

http://www.scottandmichelle.net/scot...mv?codenum=052

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 19 '05 #3
Mattias

You are a god! Thanks, just what I was looking for.

Paul.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Evgeny Zoldin | last post by:
Hi, ALL Does anybody know how to capture mouse wheel events in C# occured on the control that natively does not suppot them (e.g. MS Flex Grid)? I have code hooks windows messages in VB6. I've...
2
by: Benoit Martin | last post by:
Is it possible to detect a mouse click from a .net service. I want a process running in the background that will detect when the user clicks the mouse button (anywhere on the screen) and executes...
3
by: paul francis | last post by:
Hi Please help because I'm really stuck: I'm trying to write an application in C# which can be used to track the mouse pointer position on any window. I'm trying to use a Global mouse hook to...
0
by: John Wood | last post by:
It seems that this chap has proven this claim wrong (at least for mouse and keyboard hooks): http://www.codetools.com/useritems/globalhook.asp
3
by: Jason | last post by:
I'm trying to implement some pretty basic behavior in c#/.net 1.1 Forms but can't seem to find all the requisite pieces. When a user clicks on the background of the form, I want the app to start...
2
by: Lance | last post by:
I need to monitor the status of the mouse on an application-wide basis (i.e., not just for the control that owns the mouse). One I idea I had was to create a shared class that inherits from timer...
3
by: cichy83 | last post by:
Hi! I have a problem with global hooks under these two OS. I'm using three following types of hooks: CBT Hokk, Keyboard Hook, Mouse Hooks. I'm using BCB 6, under Win XP everything works fine...
0
by: effone | last post by:
I have two addins installed in my Outlook 2003, both have their specific functionality, I would like to integrate the functionality of both thru global mouse hooks, here is my senerio 1) Lets say...
0
by: ramon.talavera | last post by:
Hello, I am trying to read rawinput as I have to distinguish between two devices (mice), I have found several examples and I am able to detect Raw Input in the local window, but cannot...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.