Last active
January 13, 2018 11:55
-
-
Save kuchikios/208a8610a52008e389fa353dbda517a1 to your computer and use it in GitHub Desktop.
WinEventHook.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Windows.Forms; | |
using Run.OvO.Code.CommandBroker.Input.Win32; | |
namespace Run.OvO.Code.CommandBroker.Input | |
{ | |
partial class MainForm : Form | |
{ | |
private WinEventHook WinEventHook; | |
public MainForm( ) | |
{ | |
InitializeComponent( ); | |
} | |
protected override void OnShown( EventArgs e ) | |
{ | |
// WinEventHook | |
{ | |
const uint WINEVENT_SKIPOWNTHREAD = 0x0001; | |
const uint WINEVENT_SKIPOWNPROCESS = 0x0002; | |
const uint EVENT_SYSTEM_FOREGROUND = 0x0003; | |
{ | |
const uint eventMin = EVENT_SYSTEM_FOREGROUND; | |
const uint eventMax = EVENT_SYSTEM_FOREGROUND; | |
const int idProcess = 0; | |
const int idThread = 0; | |
const uint dwFlags = WINEVENT_SKIPOWNTHREAD | WINEVENT_SKIPOWNPROCESS; | |
{ | |
WinEventHook = WinEventHook.StartNew( eventMin, eventMax, idProcess, idThread, dwFlags, WinEventHook_Callback ); | |
} | |
} | |
} | |
base.OnShown( e ); | |
} | |
private void WinEventHook_Callback( uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime ) | |
{ | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Security; | |
using Microsoft.Win32.SafeHandles; | |
namespace Run.OvO.Code.CommandBroker.Input.Win32 | |
{ | |
public delegate void WinEventAction( uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime ); | |
public sealed partial class WinEventHook : IDisposable | |
{ | |
private WinEventHookHandle winEventHandle; | |
private volatile WinEventProc winEventProc; | |
private volatile WinEventAction winEventAction; | |
private WinEventHook( uint eventMin, uint eventMax, int idProcess, int idThread, uint dwFlags, WinEventAction callback ) | |
{ | |
winEventAction = callback; | |
winEventProc = WinEventHook_Proc; | |
try | |
{ | |
winEventHandle = UnsafeNativeMethods.SetWinEventHook( eventMin, eventMax, IntPtr.Zero, winEventProc, idProcess, idThread, dwFlags ); | |
if ( winEventHandle.IsInvalid ) | |
{ | |
throw new ArgumentException( ); | |
} | |
} | |
catch | |
{ | |
winEventProc = null; | |
winEventAction = null; | |
throw; | |
} | |
} | |
private void WinEventHook_Proc( IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime ) | |
{ | |
winEventAction( eventType, hwnd, idObject, idChild, dwEventThread, dwmsEventTime ); | |
} | |
private delegate void WinEventProc( IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime ); | |
private class WinEventHookHandle : SafeHandleZeroOrMinusOneIsInvalid | |
{ | |
WinEventHookHandle( ) | |
: base( true ) | |
{ | |
// | |
} | |
protected override bool ReleaseHandle( ) => UnsafeNativeMethods.UnhookWinEvent( handle ); | |
private static class UnsafeNativeMethods | |
{ | |
[SuppressUnmanagedCodeSecurity] | |
[DllImport( "user32" )] | |
[return: MarshalAs( UnmanagedType.Bool )] | |
public static extern bool UnhookWinEvent( IntPtr hWinEventHook ); | |
} | |
} | |
public static WinEventHook StartNew( uint eventMin, uint eventMax, int idProcess, int idThread, uint dwFlags, WinEventAction callback ) | |
{ | |
return new WinEventHook( eventMin, eventMax, idProcess, idThread, dwFlags, callback ); | |
} | |
private static class UnsafeNativeMethods | |
{ | |
[SuppressUnmanagedCodeSecurity] | |
[DllImport( "user32" )] | |
public static extern WinEventHookHandle SetWinEventHook( uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProc lpfnWinEventProc, int idProcess, int idThread, uint dwFlags ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
namespace Run.OvO.Code.CommandBroker.Input.Win32 | |
{ | |
public sealed partial class WinEventHook | |
{ | |
private int isDisposed; | |
public void Dispose( ) | |
{ | |
if ( 1 == Interlocked.Increment( ref isDisposed ) ) | |
{ | |
if ( null != winEventHandle ) | |
{ | |
winEventHandle.Dispose( ); | |
winEventHandle = null; | |
} | |
if ( null != winEventProc ) | |
{ | |
winEventProc = null; | |
} | |
if ( null != winEventAction ) | |
{ | |
winEventAction = null; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment