给大家一份封装Hook调用的C#类

omg_smile.gif

None.gif //  ***********************************************************************
None.gif
//   LocalWindowsHook class
None.gif
//   Dino Esposito, summer 2002
None.gif
//  
None.gif
//   Provide a general infrastructure for using Win32 
None.gif
//   hooks in .NET applications
None.gif
//  
None.gif
//  ***********************************************************************
None.gif

None.gif
None.gif
using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  Win32API
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Class HookEventArgs#region Class HookEventArgs
InBlock.gif    
public class HookEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int HookCode;    // Hook code
InBlock.gif
        public IntPtr wParam;    // WPARAM argument
InBlock.gif
        public IntPtr lParam;    // LPARAM argument
ExpandedSubBlockEnd.gif
    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Enum HookType#region Enum HookType
InBlock.gif    
// Hook Types
InBlock.gif
    public enum HookType : int
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        WH_JOURNALRECORD 
= 0,
InBlock.gif        WH_JOURNALPLAYBACK 
= 1,
InBlock.gif        WH_KEYBOARD 
= 2,
InBlock.gif        WH_GETMESSAGE 
= 3,
InBlock.gif        WH_CALLWNDPROC 
= 4,
InBlock.gif        WH_CBT 
= 5,
InBlock.gif        WH_SYSMSGFILTER 
= 6,
InBlock.gif        WH_MOUSE 
= 7,
InBlock.gif        WH_HARDWARE 
= 8,
InBlock.gif        WH_DEBUG 
= 9,
InBlock.gif        WH_SHELL 
= 10,
InBlock.gif        WH_FOREGROUNDIDLE 
= 11,
InBlock.gif        WH_CALLWNDPROCRET 
= 12,        
InBlock.gif        WH_KEYBOARD_LL 
= 13,
InBlock.gif        WH_MOUSE_LL 
= 14
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
WindowsHook#region WindowsHook
InBlock.gif    
public class WindowsHook
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Filter function delegate
InBlock.gif
        public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Internal properties
InBlock.gif
        protected IntPtr m_hhook = IntPtr.Zero;
InBlock.gif        
protected HookProc m_filterFunc = null;
InBlock.gif        
protected HookType m_hookType;
InBlock.gif        
// ************************************************************************
InBlock.gif        
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Event delegate
InBlock.gif
        public delegate void HookEventHandler(object sender, HookEventArgs e);
InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Event: HookInvoked 
InBlock.gif
        public event HookEventHandler HookInvoked;
InBlock.gif        
protected void OnHookInvoked(HookEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (HookInvoked != null)
InBlock.gif                HookInvoked(
this, e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Class constructor(s)
InBlock.gif
        public WindowsHook(HookType hook)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_hookType 
= hook;
InBlock.gif            m_filterFunc 
= new HookProc(this.CoreHookProc); 
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public WindowsHook(HookType hook, HookProc func)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_hookType 
= hook;
InBlock.gif            m_filterFunc 
= func; 
ExpandedSubBlockEnd.gif        }
        
InBlock.gif        
// ************************************************************************
InBlock.gif    
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Default filter function
InBlock.gif
        protected int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (code < 0)
InBlock.gif                
return CallNextHookEx(m_hhook, code, wParam, lParam);
InBlock.gif
InBlock.gif            
// Let clients determine what to do
InBlock.gif
            HookEventArgs e = new HookEventArgs();
InBlock.gif            e.HookCode 
= code;
InBlock.gif            e.wParam 
= wParam;
InBlock.gif            e.lParam 
= lParam;
InBlock.gif            OnHookInvoked(e);
InBlock.gif
InBlock.gif            
// Yield to the next hook in the chain
InBlock.gif
            return CallNextHookEx(m_hhook, code, wParam, lParam);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Install the hook
InBlock.gif
        public void Install()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_hhook 
= SetWindowsHookEx(
InBlock.gif                m_hookType, 
InBlock.gif                m_filterFunc, 
InBlock.gif                IntPtr.Zero, 
InBlock.gif                (
int) AppDomain.GetCurrentThreadId());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Uninstall the hook
InBlock.gif
        public void Uninstall()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UnhookWindowsHookEx(m_hhook); 
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// ************************************************************************
InBlock.gif

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Win32 Imports#region Win32 Imports
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Win32: SetWindowsHookEx()
InBlock.gif
        [DllImport("user32.dll")]
InBlock.gif        
protected static extern IntPtr SetWindowsHookEx(HookType code, 
InBlock.gif            HookProc func,
InBlock.gif            IntPtr hInstance,
InBlock.gif            
int threadID);
InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Win32: UnhookWindowsHookEx()
InBlock.gif
        [DllImport("user32.dll")]
InBlock.gif        
protected static extern int UnhookWindowsHookEx(IntPtr hhook); 
InBlock.gif        
// ************************************************************************
InBlock.gif
InBlock.gif        
// ************************************************************************
InBlock.gif        
// Win32: CallNextHookEx()
InBlock.gif
        [DllImport("user32.dll")]
InBlock.gif        
protected static extern int CallNextHookEx(IntPtr hhook, 
InBlock.gif            
int code, IntPtr wParam, IntPtr lParam);
InBlock.gif        
// ************************************************************************
ExpandedSubBlockEnd.gif
        #endregion

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}

None.gif

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值