0
HI i have a following code that reads mouse event

using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
    public enum MouseKeyType
    {
        MouseMoveOnly=512,
        MouseLeftKeyDown=513,
        MouseLeftKeyup=514,
        MouseRightKeyDown = 516,
        MouseRightKeyup = 517,
        MouseMiddleKeyDown = 519,
        MouseMiddleKeyup = 520,
        MouseScroll=522
    }
    [StructLayout(LayoutKind.Sequential)]
    public class MouseEventData
    {
        public Point point;
        private int mouseData;
        private int flags;
        public int time;
        //public int dwExtraInfo;
        public bool IsScrollUp
        {
            get
            {
                return mouseData > -1;
            }
        }
        public MouseKeyType mouseKeyType;
    }
    public class gc
    {
        // Event
        public delegate void gcMouseEvents(object source, MouseEventData e);
        public event gcMouseEvents gcevent;
        // WIN32 hook
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        private static extern int SetWindowsHookEx( int idHook, HookProc lpfn,  IntPtr hMod, int dwThreadId);

        //just a delegate to simplify the type of 
        private delegate int HookProc(int nCode,int wParam, IntPtr lParam);
        private HookProc _hookCallback;
        public gc()
        {
            _hookCallback = new HookProc((x,y,z)=> 
            {
                //string output = JsonConvert.SerializeObject(ptr);
                //Console.WriteLine(output);
                if (gcevent!=null)
                {
                    MouseEventData ptr = (MouseEventData)Marshal.PtrToStructure(z, typeof(MouseEventData));
                    ptr.mouseKeyType = (MouseKeyType)y;
                    gcevent(this, ptr);
                }
                return 0;
            });

              SetWindowsHookEx(
                14,
                _hookCallback,
                Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                0);

        }
    }

This seems to work fine in a form application when code placed in that project. BUt when code is placed in an console library and and a reference added then it dosent work.. I tried adding dlls to windows form etc and played with static etc but nothing seems to work when its placed in a dll

kunal verma
  • 456
  • 5
  • 13
  • A console app is not really designed for mouse input. I think @Frank Krueger ‘s comment is worth considering. His comment is at the SO question… [Console App Mouse-Click X Y Coordinate Detection/Comparison](https://stackoverflow.com/questions/1944481/console-app-mouse-click-x-y-coordinate-detection-comparison) you may find it helpful. – JohnG Dec 24 '19 at 05:50
  • [SetWindowsHookExW](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexw). Read the description related to the `hMod` and and `dwThreadId`. These work together. See also: [Using Hooks](https://learn.microsoft.com/en-us/windows/win32/winmsg/using-hooks) and [Low-Level Keyboard Hook in C# (Console app)](https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/) (the same for `WH_MOUSE_LL`). It also appears that you're missing many `pieces` in that code. What's the use of this application? Maybe you wanted a build a `.dll` instead? – Jimi Dec 24 '19 at 06:02
  • Take the declarations from here: [SetWindowsHookEx WH_KEYBOARD_LL not getting events](https://stackoverflow.com/a/1746409/7444103) – Jimi Dec 24 '19 at 06:08
  • @Frank Krueger i created a C# library and moved this code there and added its reference in WINDOWS form application – kunal verma Dec 24 '19 at 06:36
  • @Jimi yes ...I was trying to include the code in dll and and refernece in win form apaloication to use it ...but it dosent seems to work – kunal verma Dec 24 '19 at 06:37
  • 2
    Build a `dll` then, not a Console app. Read carefully the Docs about those two parameters (`hMod` and `dwThreadId`). These work differently when used in a library (as clearly described in the Docs). Note that the Docs state that calling `CallNextHookEx()` is *Optional*. It's not, actually. See the implementation I linked before and also this one: [Capture all Windows Messages](https://stackoverflow.com/a/11361996/7444103) (in relation to releasing the allocated resources/handlers). – Jimi Dec 24 '19 at 06:56

0 Answers0