C#实现全局钩子的一点问题
我写了一些代码,编译的时候说setwindowsex可访问性不一致,应该如何解决?(由于字数限制,代码不完整)usingSystem;usingSystem.Collec...
我写了一些代码,编译的时候说setwindowsex可访问性不一致,应该如何解决?(由于字数限制,代码不完整)
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;
using System.Reflection;
using System.Diagnostics;
namespace hook
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 钩子类型枚举
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,
MouseLL = 14,
};
#endregion
IntPtr _nextHookPtr; //记录Hook编号
#region 必须的API
[DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int SetWindowsHookEx(int idHook,HookProc lpfn,IntPtr hMod,int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhook);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
#endregion
internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam); //实现的委托
public IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
//...你的代码
if (code < 0)
{
return CallNextHookEx(_nextHookPtr, code, wparam, lparam); //返回,让后面的程序处理该消息
}
if (wparam.ToInt32()==(int)Keys.A)
{
//to do
return (IntPtr)1; //直接返回了,该消息就处理结束了
}
else
{
return IntPtr.Zero;
}
}
public void SetHook()
{
if( _nextHookPtr != IntPtr.Zero ) //已经勾过了
{
return;
}
HookProc myhookProc = new HookProc(MyHookProc); //声明一个自己的Hook实现函数的委托对象
_nextHookPtr = SetWindowsHookEx(13, myhookProc,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //加到Hook链中
if (_nextHookPtr == IntPtr.Zero)
{
MessageBox.Show("安装钩子失败!");
}
} 展开
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;
using System.Reflection;
using System.Diagnostics;
namespace hook
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 钩子类型枚举
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,
MouseLL = 14,
};
#endregion
IntPtr _nextHookPtr; //记录Hook编号
#region 必须的API
[DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int SetWindowsHookEx(int idHook,HookProc lpfn,IntPtr hMod,int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhook);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);
#endregion
internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam); //实现的委托
public IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
//...你的代码
if (code < 0)
{
return CallNextHookEx(_nextHookPtr, code, wparam, lparam); //返回,让后面的程序处理该消息
}
if (wparam.ToInt32()==(int)Keys.A)
{
//to do
return (IntPtr)1; //直接返回了,该消息就处理结束了
}
else
{
return IntPtr.Zero;
}
}
public void SetHook()
{
if( _nextHookPtr != IntPtr.Zero ) //已经勾过了
{
return;
}
HookProc myhookProc = new HookProc(MyHookProc); //声明一个自己的Hook实现函数的委托对象
_nextHookPtr = SetWindowsHookEx(13, myhookProc,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); //加到Hook链中
if (_nextHookPtr == IntPtr.Zero)
{
MessageBox.Show("安装钩子失败!");
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询