[Mono-list] Low level mouse hook via SetWindowsHookEx

Jonathan Pryor jonpryor at vt.edu
Sat Jul 4 17:56:27 EDT 2009


For the love of $deity, you shouldn't do this on Windows either.

The problem is that hooks involve inserting your DLL into every  
process' address space, which will thus require loading the CLR.  
However, until .NET 4.0 (unreleased), only one version of the CLR can  
be loaded. Result: you may try loading your assembly into a process  
which has only .NET 1.x loaded, resulting in a TypeLoadException.

This is, of course, bad. It's also why you don't currently see many  
Explorer shell extensions written on managed code.

Please don't do that.

(Plus, it's not portable to non-Windows platforms...)

  - Jon

On Jul 4, 2009, at 4:40 PM, cstockton <chrisstocktonaz at gmail.com> wrote:

>
> Hello,
>
> I am porting a app I want to distribute for free in Linux. It clicks  
> for
> users with RSI (Repetitive Stress Syndrome).
>
> Part of the application is monitoring the movement of the mouse and  
> sending
> global clicks, this is window independent. I achieved this in C# under
> windows using :
>
> [code]
>        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
> true)]
>
>        private static extern IntPtr SetWindowsHookEx(int idHook,
> LowLevelProc lpfn, IntPtr hMod, uint dwThreadId);
>
>
>        ... (more imports)
>
>        [DllImport("user32.dll")]
>
>        static extern void mouse_event(uint dwFlags, uint dx, uint  
> dy, uint
> dwData, int dwExtraInfo);
>
>        // later I
>        SetWindowsHookEx(WH_MOUSE_LL, proc,
> GetModuleHandle(curModule.ModuleName), 0);
>
> [/code]
>
> The app is complete and runs great in windows, it builds okay in  
> MonoDevelop
> but I get a exception when running :
>
> Unhandled Exception: System.EntryPointNotFoundException:  
> GetModuleHandle
>  at (wrapper managed-to-native) Clicker.Form1:GetModuleHandle (string)
>  at Clicker.Form1.SetMouseHook (Clicker.LowLevelProc proc) [0x00014]  
> in ..
> Form1.cs:173
>  at Clicker.Form1..ctor () [0x004d2] in .. Form1.cs:510
>  at (wrapper remoting-invoke-with-check) Clicker.Form1:.ctor ()
>  at Clicker.Program.Main () [0x00015] in .. Program.cs:18
>
> I am assuming I am missing some key steps in those unmanaged  
> invokes, but am
> unsure (after much much much googling) how to achieve this. If I  
> need to
> scrap the hooking into the user32/kernel32 and do the alternative in  
> X11
> libraries of some sort I am okay with that.
>
> -Chris
>
> -- 
> View this message in context: http://www.nabble.com/Low-level-mouse-hook-via-SetWindowsHookEx-tp24337660p24337660.html
> Sent from the Mono - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list


More information about the Mono-list mailing list