学习hook,看到这个例子,说是输入b,结果输出是a,调试成功,但是运行输入什么输出还是什么,怎么回事呀,好像钩不到。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace ABturn
{
public partial class Form1 : Form
{
internal enum HookType
{
//msgfilter=-1,
//journalrecord=0,
//journalplayback=1,
Keyboard = 2,
//getmessage=3,
//callwndproc = 4,
//cbt = 5,
//sysmsgfilter = 6,
//mouse = 7,
//hardware = 8,
//debug = 9,
//shell = 10,
//foregroundidle = 11,
//callwndprocret = 12,
//keyboardll = 13,
};
IntPtr _nextHookPtr; //记录hook编号
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); //取得当前线程编号的API [DllImport("user32.dll")]
internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消hook的api [DllImport("user32.dll")]
internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置hook的api [DllImport("user32.dll")]
internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam);
internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
if (code < 0)
return CallNextHookEx(_nextHookPtr, code, wparam, lparam);
if (wparam.ToInt32() == 98 || wparam.ToInt32() == 66)
{
this.textBox1.Text = "a";
return (IntPtr)1;
}
else
{
return IntPtr.Zero;
}
} public void SetHook()
{
if (_nextHookPtr != IntPtr.Zero)
return; HookProc myhookProc = new HookProc(MyHookProc);
_nextHookPtr = SetWindowsHookEx((int)HookType.Keyboard , myhookProc, IntPtr.Zero, GetCurrentThreadId());
}
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr);
_nextHookPtr = IntPtr.Zero;
}
}
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
SetHook();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnHook();
} }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace ABturn
{
public partial class Form1 : Form
{
internal enum HookType
{
//msgfilter=-1,
//journalrecord=0,
//journalplayback=1,
Keyboard = 2,
//getmessage=3,
//callwndproc = 4,
//cbt = 5,
//sysmsgfilter = 6,
//mouse = 7,
//hardware = 8,
//debug = 9,
//shell = 10,
//foregroundidle = 11,
//callwndprocret = 12,
//keyboardll = 13,
};
IntPtr _nextHookPtr; //记录hook编号
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); //取得当前线程编号的API [DllImport("user32.dll")]
internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消hook的api [DllImport("user32.dll")]
internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置hook的api [DllImport("user32.dll")]
internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam);
internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam);
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
if (code < 0)
return CallNextHookEx(_nextHookPtr, code, wparam, lparam);
if (wparam.ToInt32() == 98 || wparam.ToInt32() == 66)
{
this.textBox1.Text = "a";
return (IntPtr)1;
}
else
{
return IntPtr.Zero;
}
} public void SetHook()
{
if (_nextHookPtr != IntPtr.Zero)
return; HookProc myhookProc = new HookProc(MyHookProc);
_nextHookPtr = SetWindowsHookEx((int)HookType.Keyboard , myhookProc, IntPtr.Zero, GetCurrentThreadId());
}
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr);
_nextHookPtr = IntPtr.Zero;
}
}
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
SetHook();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnHook();
} }
}
2、放在form_load中,应加入什么语句,使程序可行