跟着老侯玩编程 跟着老侯玩编程
首页
  • 基础语法
  • 设计模式
  • 基础语法
  • 课程目录
  • 码农宝典
网盘 (opens new window)
留言
首页
  • 基础语法
  • 设计模式
  • 基础语法
  • 课程目录
  • 码农宝典
网盘 (opens new window)
留言
  • 基础语法

    • 单元文件
    • 变量和常量
    • 内联变量
    • 基本数据类型
    • 复合数据类型
    • 语句(选择语句)
    • 语句(循环语句)
    • 数组
    • 初识例程
    • 初识面向对象
    • 属性Property
    • 面向对象的方法
    • 指针
    • 接口
    • 匿名函数和委托实现
    • 多态
    • 字符串详解
    • 异常处理
    • 反射机制
    • 泛型容器
    • JSON
    • 文件操作
    • Dll创建并调用
    • DLL初始化和退出处理
    • Package
    • 理解消息循环
    • VCL与Windows消息
    • 钩子原理
      • 自定义消息拦截
    • 进程通讯-内存映像
    • 多线程开篇
    • 线程控制
    • 线程同步
  • 设计模式

    • 单例模式
    • 单例模式二
    • 策略模式
    • 简单工厂模式
    • 建造者模式
    • 原型模式
    • 模板方法模式
    • 状态模式
    • 迭代器模式
    • 解释器模式
    • 责任链模式
    • 中介者模式
    • 备忘录模式
    • 命令模式
    • 观察者模式
    • 访问者模式
    • 适配器模式
    • 桥接模式
  • Delphi
  • 基础语法
舞动的代码
2022-05-05

钩子原理

此篇代码是在读完【Delphi高手突破】中关于消息处理一章之后尝试的代码,保留下来了

# 自定义消息拦截

unit UnitMessageHook;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

const
  {定义用于HOOK的消息,也可以是Windows的标准消息}
  WM_TestMessage = WM_USER + 2000;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
var
  HookHandle: HHOOK;

  {Hook处理函数}
function MyFunctionHookPro(Code: Integer; WParam: LongInt; Msg: LongInt): LongInt; stdcall;
begin
  if (Code = HC_ACTION) then
  begin
    if PMsg(Msg)^.message = WM_TestMessage then
    begin
      ShowMessage('已经截获该消息' + PChar(PMsg(Msg)^.WParam));
    end;

  end;
  Result := CallNextHookEx(HookHandle, Code, WParam, LongInt(@Msg));
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  msg: string;
begin
  msg := 'Hello World!';
  {发送特定消息}
  PostMessage(Self.Handle, WM_TestMessage, Integer(PChar(mess)), 0);
end;

{挂钩}
procedure TForm1.FormCreate(Sender: TObject);
begin
  HookHandle := SetWindowsHookEx(WH_GETMESSAGE, @MyFunctionHookPro, HInstance, GetCurrentThreadId);
end;

{摘钩}
procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnhookWindowsHookEx(HookHandle);
end;

end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

以上代码会造成由于内存空间释放而产生乱码

优化代码如下

procedure Send();
var
  mess: string;
  ps: PString;
begin
  New(ps);
  mess := 'Hello World!';
  ps^ := mess;
  {发送特定消息}
  PostMessage(Self.Handle, WM_TestMessage, Integer(PChar(ps)), 0
end;

//接收端
PS := PString(PMsg(Msg)^.WParam);
mess := PS^;
//Do Something
ShowMessage('已经截获该消息' + mess);

//加这一句会导致程序崩溃
Dispose(PS);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
上次更新: 2022/05/05, 14:24:28
VCL与Windows消息
进程通讯-内存映像

← VCL与Windows消息 进程通讯-内存映像→

Theme by Vdoing | Copyright © 2013-2022 冀ICP备16006233号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×