使用WindowsAPI函数 以及使用钩子
来源:互联网 发布:淘宝二级域名开通 编辑:程序博客网 时间:2024/05/04 05:02
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestHook
{
public partial class Form1 : Form
{
#region 第一步:声明API函数
//使用钩子,需要使用WindowsAPI函数,所以要先声明这些API函数。
// 安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
// 卸载钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
// 继续下一个钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得当前线程编号
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
//// 全局钩子
//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//SetWindowsHookEx( 13, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0)
#endregion
#region 第二步:声明,定义委托
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;//如果hKeyboardHook不为0则说明钩子安装成功
HookProc KeyboardHookProcedure;
#endregion
#region 第三步:编写钩子子程
//钩子子程就是钩子所要做的事情。
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr IParam)
{
if (nCode >= 0)
{
textBox1.Text = "hello,fangqm.cn";
return 1;
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, IParam);
}
#endregion
#region 第四步:正式启用钩子
//钩子安装
public void HookStart()
{
if (hKeyboardHook == 0)//如果hKeyboardHook==0,钩子安装失败
{
//创建HookProc实例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//设置线程钩子
hKeyboardHook = SetWindowsHookEx(2, KeyboardHookProc, IntPtr.Zero, GetCurrentThreadId());
if (hKeyboardHook == 0)
{
//终止钩子
throw new Exception("安装钩子失败");
}
}
}
//钩子卸载
public void HookStop()
{
bool retKeyboard = true;
if (hKeyboardHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
if (!retKeyboard)
throw new Exception("钩子卸载失败");
}
#endregion
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.HookStart();
}
private void button2_Click(object sender, EventArgs e)
{
this.HookStop();
}
public struct MSG
{
public Point p;
public IntPtr HWnd;
public uint wHitTestCode;
public int dwExtraInfo;
}
public struct KeyMSG
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
// MSG m = (MSG)Marshal.PtrToStructure(lParam, typeof(MSG));
// KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
// // 按下的键
// Keys keyData = (Keys)wParam;
//if(lParam.ToInt32() > 0)
//{
// // 键盘按下
//}
//if(lParam.ToInt32() < 0)
//{
// // 键盘抬起
//}
}
}
- 使用WindowsAPI函数 以及使用钩子
- 钩子函数使用小结
- 钩子函数的使用
- 使用WindowsAPI读写配置文件
- 使用jinvoke调用windowsApi
- 使用钩子函数的注意事项
- 使用WindowsAPI取剪贴板文本
- 使用WindowsAPI置剪贴板文本
- 使用钩子
- 使用钩子
- 钩子使用
- 子类化GetOpenFileName/GetSaveFileName, 以及钩子函数OFNHookProc的使用的简要说明
- 钩子技术介绍及函数使用
- 钩子技术介绍及函数使用
- 使用钩子函数截取Windows应用程序口令
- 在Wince下使用钩子函数
- 钩子技术介绍及函数使用
- 钩子技术介绍及函数使用
- mysql正确关闭slave步骤
- caffe-face编译
- 使用nmonchart把.nmon文件转换成html
- python正则表达式学习 笔记
- Ubuntu输入法安装及图标键盘显示
- 使用WindowsAPI函数 以及使用钩子
- 让input控件,填充满td
- oracle数据库oracle listener refused12514 the connection with
- 关于Apple 的Analytics
- MixIn for Python
- tensorflow function笔记: tf.tile
- N中类型每种M样--不同的钱数--牌型种数--抽签
- error C2664: 'LONG CTabCtrl::InsertItem(int,TCITEMW *)' : cannot convert parameter 2 from 'char *' t
- JavaEE容器介绍