473,516 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12571
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
6532
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 translated it into C#, but it does not work :-( Any another idea? Thanks
2
4863
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 an action accordingly. All the event handlers for the mouse that I see are for Windows Forms. Is there a way to do it without any controls on the...
3
454
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 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...
0
1188
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
14132
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 receiving MouseMove updates -- whether the pointer is over the form or not (this being the crucial part) -- until the next MouseDown. It's easy to...
2
1833
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 and tests Windows.Forms.Control.MouseButtons and Windows.Forms.Cursor.Current.Position in the tick event. Obviously this technique is not perfect...
3
2237
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 (functions are in dll of course, etc.). But under Win 98 my program (without any communicates) execute normal, but not working at all.
0
2173
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 there is a button "abc" on toolbar "A" in outlook mail composing window 2) and there is a button "xyz" on toolbar "X" in the same mail composing...
0
2622
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 intecept raw input messages in other windows, so I cannot track the mouse as soon as another window catches the mouse focus.
0
7412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7549
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5717
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3265
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
494
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.