标题:求高人指教(钩子的问题)
只看楼主
chenjin1
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-5-15
 问题点数:0 回复次数:3 
求高人指教(钩子的问题)
我做了个钩子,钩进了记事本,但截取不到消息,提示说"给内存不可读",我该怎么办???
搜索更多相关主题的帖子: 钩子 高人 指教 
2007-10-22 23:28
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
得分:0 
没处理好,你的代码呢?

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-10-23 12:33
没有注册
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-8-16
得分:0 

楼主

问下你真名叫chenjin?

2007-10-23 17:35
chenjin1
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-5-15
得分:0 

Class1.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MyHook
{
public class Class1
{
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
};
IntPtr _nextHookPtr; //记录Hook编号
int toda = 1;
bool da = false;
[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); //取得下一个Hook的API
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //取得指定窗口的句柄
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID); //取得创建窗口的线程ID
public string str;

public IntPtr SetHook(int dwThreadid,HookProc myhookproc)
{
if (_nextHookPtr != IntPtr.Zero) //已经勾过了
return (IntPtr)0;
return SetWindowsHookEx((int)HookType.GetMessage, myhookproc, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), dwThreadid);
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr); //从Hook链中取消
_nextHookPtr = IntPtr.Zero;
}
}
public IntPtr gethandle(string fname)
{
return FindWindow(null, fname);
}
public int getPID(IntPtr hwnd)
{
return GetWindowThreadProcessId(hwnd, 0);
}
public IntPtr callnext(IntPtr handle, int code, IntPtr wparam, IntPtr lparam)
{
return CallNextHookEx( handle,code, wparam, lparam);
}
}
}
Form1.cs
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.Diagnostics;
using MyHook;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Class1.HookProc mouseHookCallBack;
private IntPtr _nextHookPtr;
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
Class1 myhook = new Class1();
if (code < 0) return myhook.callnext(_nextHookPtr, code, wparam, lparam); //返回,让后面的程序处理该消息
this.textBox1.Text += (char)wparam;
return (IntPtr)0; // 返回,让后面的程序处理该消息
}

private void Form1_Load(object sender, EventArgs e)
{
Class1 myhook = new Class1();
mouseHookCallBack = (MyHook.Class1.HookProc)MyHookProc;
IntPtr hWnd;
hWnd = myhook.gethandle( "金山词霸 2005");
textBox2.Text = hWnd.ToString();
int ID = myhook.getPID(hWnd);
_nextHookPtr=myhook.SetHook(ID, mouseHookCallBack);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Class1 myhook = new Class1();
myhook.UnHook();
}

2007-10-24 21:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-179934-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013430 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved