home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / ZCommon / WindowsHook.cs < prev    next >
Encoding:
Text File  |  2002-06-10  |  3.6 KB  |  136 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace ZCommon
  7. {
  8.     /// <summary>
  9.     /// Implements base functionality required for setting Windows Hooks (SetWindowsHookEx API)
  10.     /// </summary>
  11.     /// <example>
  12.     /// 
  13.     /// //Sample hook that raises Idle event when thread is about to go idle.
  14.     /// //Can be usefull for procesing background lo priority tasks
  15.     ///     public class IdleHook : WindowsHook
  16.     ///     {
  17.     ///         public void Start()
  18.     ///         {
  19.     ///             UnHookMe();
  20.     ///             HookMe(11);//WH_FOREGROUNDIDLE
  21.     ///         }
  22.     ///   
  23.     ///         public void Stop()
  24.     ///         {
  25.     ///             UnHookMe();
  26.     ///         }
  27.     /// 
  28.     ///         public event EventHandler Idle;
  29.     /// 
  30.     ///         protected override Int32 HookCallBack(Int32 ncode, Int32 wParam, Int32 lParam)
  31.     ///         {
  32.     ///             if( Idle != null )
  33.     ///                 Idle(this, EventArgs.Empty);
  34.     /// 
  35.     ///             return base.HookCallBack(ncode, wParam, lParam);
  36.     ///         }
  37.     ///     }
  38.     /// </example>
  39.     abstract public class WindowsHook : MarshalByRefObject, IDisposable 
  40.     {
  41.         WindowsHook.WindowsHookCallBack hookDelegate;
  42.         IntPtr hhook;
  43.  
  44.         /// <summary>
  45.         ///    Override this function to process hooks
  46.         /// </summary>
  47.         /// <remarks>
  48.         /// Call <code>return base.HookCallBack(ncode, wParam, lParam)</code> at the end.
  49.         /// </remarks>
  50.         protected virtual Int32 HookCallBack(Int32 ncode, Int32 wParam, Int32 lParam)
  51.         {
  52.             GC.KeepAlive(this);//FxCop
  53.             /// making sure that other hooks get chance to process event as well.
  54.             return CallNextHookEx(hhook , ncode, wParam, lParam);
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Hooks <code>HookCallBack</code> method to process event of type specified by <code>idHook</code>;
  59.         /// </summary>
  60.         /// <param name="idHook">See winuser.h for possible values(WH_something)</param>
  61.         protected void HookMe(Int32 idHook)
  62.         {
  63.             GC.KeepAlive(this);
  64.             hookDelegate = new WindowsHookCallBack(hookCallBack);
  65.  
  66.             hhook = SetWindowsHookEx( 
  67.                 idHook,
  68.                 hookDelegate,
  69.                 IntPtr.Zero,
  70.                 Win32.GetCurrentThreadId() );    
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Stops processing events.
  75.         /// </summary>
  76.         protected void UnHookMe()
  77.         {
  78.             GC.KeepAlive(this);
  79.             if( hhook != IntPtr.Zero )
  80.             {
  81.                 UnhookWindowsHookEx( hhook );
  82.                 hhook = IntPtr.Zero;
  83.             }
  84.  
  85.             if( hookDelegate != null )
  86.                 hookDelegate = null;                
  87.         }
  88.  
  89.         /// <summary>
  90.         /// Stops processing events, disposes of resources.
  91.         /// </summary>
  92.         public virtual void Dispose()
  93.         {
  94.             Dispose(true);
  95.             GC.SuppressFinalize(this);
  96.         }
  97.  
  98.         protected virtual void Dispose(bool disposing)
  99.         {
  100.             UnHookMe();
  101.         }
  102.  
  103.         ~WindowsHook()
  104.         {
  105.             Dispose(false);
  106.         }
  107.  
  108.         /// <summary>
  109.         /// WindowsHook callback prototype.
  110.         /// </summary>
  111.         delegate Int32 WindowsHookCallBack(Int32 ncode, Int32 wParam, Int32 lParam);
  112.  
  113.         /// <summary>
  114.         /// Generic implementation of WindowsHook.
  115.         /// </summary>
  116.         /// <remarks>
  117.         /// Checks ncode to be greater or equal to HC_ACTION before calling virtual HookCallBack.
  118.         /// </remarks>
  119.         Int32 hookCallBack(Int32 ncode, Int32 wParam, Int32 lParam)
  120.         {
  121.             GC.KeepAlive(this);
  122.             return ncode >= 0 ?    //HC_ACTION, etc
  123.                 HookCallBack( ncode, wParam, lParam ) :
  124.                 CallNextHookEx(hhook , ncode, wParam, lParam);
  125.         }
  126.  
  127.         [DllImport("user32.dll")]
  128.         static extern IntPtr SetWindowsHookEx( Int32 idHook, WindowsHookCallBack lpfn, IntPtr hmodule, Int32 threadID);
  129.  
  130.         [DllImport("user32.dll")]
  131.         static extern Int32 CallNextHookEx(IntPtr hhok, Int32 ncode, Int32 wParam, Int32 lParam);
  132.  
  133.         [DllImport("user32.dll")]
  134.         static extern bool UnhookWindowsHookEx( IntPtr hhook );
  135.     }
  136. }