如何使用Intptr (USER32.dll)从鼠标事件中获取进程?



我有一个问题,在过去的两天里,我想得到他点击的用户的进程。比如如果用户点击记事本,我的程序应该告诉我用户点击了记事本。打开记事本。如果用户点击计算器,我的程序也会告诉用户点击计算器。计算器进程正在运行。

为了这个目的,我使用了下面的代码。钩子管理器,它给我鼠标点击事件,但不给我进程。

我只得到鼠标intptr事件。

private static void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
     Console.WriteLine("Event {0}", hwnd);// it is giving me mouse event 
     /*uint pid;
     GetWindowThreadProcessId(hwnd, out pid);// it gives me process id
     Process p = Process.GetProcessById((int)pid);// now here exception occured not in vs studio but when i run its exe then its gives me access violation exception
     if (!my.ContainsKey(p.MainWindowTitle.ToString()))
     {
          my.Add(p.MainWindowTitle.ToString(), p.Id.ToString());
          Console.WriteLine("rn");
          Console.WriteLine("Status = Running");
          Console.WriteLine("rn Window Title:" + p.MainWindowTitle.ToString());
          Console.WriteLine("rn Process Name:" + p.ProcessName.ToString());
          Console.WriteLine("rn Process Starting Time:" + p.StartTime.ToString());
     }*/
}

完整的代码是

    static void Main(string[] args)
    {
        HookManager.SubscribeToWindowEvents();
        EventLoop.Run();
    }

    public static class HookManager
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
        public static void SubscribeToWindowEvents()
        {
            if (windowEventHook == IntPtr.Zero)
            {
                windowEventHook = SetWinEventHook(
                    EVENT_SYSTEM_FOREGROUND, // eventMin
                    EVENT_SYSTEM_FOREGROUND, // eventMax
                    IntPtr.Zero,             // hmodWinEventProc
                    WindowEventCallback,     // lpfnWinEventProc
                    0,                       // idProcess
                    0,                       // idThread
                    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
                if (windowEventHook == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
        }
        static Dictionary<string, string> my = new Dictionary<string, string>();

        private static void WindowEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            Console.WriteLine("Event {0}", hwnd);
            /*uint pid;
            GetWindowThreadProcessId(hwnd, out pid);
            Process p = Process.GetProcessById((int)pid);
            if (!my.ContainsKey(p.MainWindowTitle.ToString()))
            {
                my.Add(p.MainWindowTitle.ToString(), p.Id.ToString());
                Console.WriteLine("rn");
                Console.WriteLine("Status = Running");
                Console.WriteLine("rn Window Title:" + p.MainWindowTitle.ToString());
                Console.WriteLine("rn Process Name:" + p.ProcessName.ToString());
                Console.WriteLine("rn Process Starting Time:" + p.StartTime.ToString());

            }*/
        }
    }
    private static IntPtr windowEventHook;
    private delegate void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr SetWinEventHook(int eventMin, int eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, int dwflags);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern int UnhookWinEvent(IntPtr hWinEventHook);
    private const int WINEVENT_INCONTEXT = 4;
    private const int WINEVENT_OUTOFCONTEXT = 0;
    private const int WINEVENT_SKIPOWNPROCESS = 2;
    private const int WINEVENT_SKIPOWNTHREAD = 1;
    private const int EVENT_SYSTEM_FOREGROUND = 3;

    public static class EventLoop
    {
        public static void Run()
        {
            MSG msg;
            while (true)
            {
                if (PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE))
                {
                    if (msg.Message == WM_QUIT)
                        break;
                    TranslateMessage(ref msg);
                    DispatchMessage(ref msg);
                }
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MSG
        {
            public IntPtr Hwnd;
            public uint Message;
            public IntPtr WParam;
            public IntPtr LParam;
            public uint Time;
        }
        const uint PM_NOREMOVE = 0;
        const uint PM_REMOVE = 1;
        const uint WM_QUIT = 0x0012;
        [DllImport("user32.dll")]
        private static extern bool PeekMessage(out MSG lpMsg, IntPtr hwnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
        [DllImport("user32.dll")]
        private static extern bool TranslateMessage(ref MSG lpMsg);
        [DllImport("user32.dll")]
        private static extern IntPtr DispatchMessage(ref MSG lpMsg);
    }
}

如果你想把你的逻辑在鼠标点击处理程序,你可以简单地调用GetActiveWindow来获得窗口句柄(如果你还没有)。然后你可以使用GetWindowThreadProcessId从窗口句柄中获取进程id。

每次点击鼠标都这样做看起来有点过头了。您可能应该考虑挂钩活动窗口更改。请检查此详细信息:活动窗口上的Windows系统事件是否已更改?

最新更新