I am trying to intercept global events in Windows (with the goal of writing some fancy Hotkey helper, that also inludes mouse and pen events).

I have a windows forms Application and in the Constructor I am trying to do this:

using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule) {
    IntPtr hInstance = GetModuleHandle(curModule.ModuleName);
    hookId = SetWindowsHookEx(3, hookProc, hInstance, 0);
}

My test hook behind hookProc is very simple:

 private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 {
     // Nothing so far
     return CallNextHookEx(hookId, nCode, wParam, lParam);
 }

Now, using breakpoints I found out that this app does not capture any events ...

Poking around the internet a bit more, I got this:

IntPtr hInstance =LoadLibrary("User32");
hookId = SetWindowsHookEx(3, hookProc, hInstance, 0);

Doing this my windows crashes forcing me to restart using the task manager :).

Any Idea what I am doing wrong? What exactly does hInstance have to be, so that I can capture gobal events?

Here is the complete Code of the forms application:

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetHook(HookCallback);
        }

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

        private static IntPtr hookId = IntPtr.Zero;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        private void SetHook(HookProc hookProc)
        {
            using (var curProcess = System.Diagnostics.Process.GetCurrentProcess())
            using (var curModule = curProcess.MainModule) {
                //IntPtr hInstance =LoadLibrary("User32");
                IntPtr hInstance = GetModuleHandle(curModule.ModuleName);
                hookId = SetWindowsHookEx(3, hookProc, hInstance, 0);
            }
        }

        private static int count = 0;
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            // Nothu
            Debug.WriteLine($"event {++count}");
            return CallNextHookEx(hookId, nCode, wParam, lParam);
        }

    }
}
0

There are 0 best solutions below