public void Attatch() { IntPtr cbPtr = Marshal.GetFunctionPointerForDelegate(callback); hook = SetWinEventHook(WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, cbPtr, 0, 0, WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT | WindowsEventHookFlags.WINEVENT_SKIPOWNPROCESS); Debug.WriteLine("WinEventHook installed"); }
public void SetHook(string windowName) { if (hHook != null && !hHook.IsInvalid && !hHook.IsClosed) { hHook.Close(); callback = null; } callback = WinEventProc(windowName); hHook = SetWinEventHook(WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, WindowsEventHookType.EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, callback, 0, 0, WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT | WindowsEventHookFlags.WINEVENT_SKIPOWNPROCESS); var err = Marshal.GetLastWin32Error(); if (hHook == null || hHook.IsInvalid) { System.Console.WriteLine("Hook set failed..."); System.Console.WriteLine($"error code: {err}"); } }