【控件功能】我来做练习-第57课-Hook Windows API ,Hook COM,Hook Method

本文介绍如何使用Delphi实现Windows API、COM对象和方法的钩子技术,包括Hook Windows API、Hook COM接口及Hook对象方法的示例代码,展示了在不同场景下进行钩子操作的方法。
摘要由CSDN通过智能技术生成

Hook Windows API ,Hook COM,Hook Method

` 提示:Hook Windows API ,Hook COM,Hook Method

请添加图片描述

核心源码

uses
ComObj, ShlObj, HookIntfs, HookUtils;

var
MessageBoxNext: function (hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;

function MessageBoxCallBack(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;
var
S: string;
begin
if Copy(lpText, 1, 5) = ‘hello’ then
S := ‘我把 hello 开头的文字改成现在的样子了’
else S := lpText;
Result := MessageBoxNext(hWnd, PChar(S), lpCaption, uType);
end;

var
ShellLink: IShellLink;
ShellLinkSetPathNext: function(Self: IShellLink; pszFile: LPTSTR): HResult; stdcall;

function ShellLinkSetPathCallBack(Self: IShellLink; pszFile: LPTSTR): HResult; stdcall;
begin
ShowMessage(Format(‘你调用到 ISHellLink($%x) 的 SetPath 方法了,参数 “%s”’,
[NativeInt(Pointer(Self)), string(pszFile)]));
Result := ShellLinkSetPathNext(Self, ‘d:\Windows’);
end;

var
ObjectFreeInstanceNext: procedure(Self: TObject);

procedure ObjectFreeInstanceCallBack(Self: TObject);
begin
if Self <> nil then
OutputDebugString(PChar(Format(‘“%s” 实例 [%x] 被释放!’, [Self.ClassName,
NativeInt(Self)])));
ObjectFreeInstanceNext(Self);
end;

{ TMainForm }

procedure TMainForm.cbHookAPIClick(Sender: TObject);
const
{ KaTeX parse error: Expected 'EOF', got '}' at position 14: IFDEF UNICODE}̲ MessageBoxPr…ELSE}
MessageBoxProcName = ‘MessageBoxA’;
{$ENDIF}
begin
if TCheckBox(Sender).Checked then
begin
// 测试API钩子,MessageBox,因为我是Unicode版本Delphi
if not Assigned(MessageBoxNext) then
begin
// 重绘,画出来的文字就会变样了.
HookProc(user32, MessageBoxProcName, @MessageBoxCallBack, @MessageBoxNext);
end
else
begin
ShowMessage(‘钩过了’);
end;
end
else
begin
if Assigned(MessageBoxNext) then
UnhookProc(@MessageBoxNext);
@MessageBoxNext := nil;
end;
// 触发 MessageBox API 调用
MessageBox(Handle, ‘hello world!’, ‘’, 0);
end;

procedure TMainForm.cbHookCOMClick(Sender: TObject);
begin
if TCheckBox(Sender).Checked then
begin
if not Assigned(ShellLinkSetPathNext) then
begin
HookInterface(ShellLink, 20, @ShellLinkSetPathCallBack, @ShellLinkSetPathNext);
ShellLink.SetPath(‘c:\Windows’);
end
else
begin
ShowMessage(‘钩过了’);
end;
end
else
begin
if Assigned(ShellLinkSetPathNext) then
UnhookProc(@ShellLinkSetPathNext);
@ShellLinkSetPathNext := nil;
end;
end;

procedure TMainForm.cbHookObjectClick(Sender: TObject);
begin
if TCheckBox(Sender).Checked then
begin
if not Assigned(ObjectFreeInstanceNext) then
begin
HookProc(@TObject.FreeInstance, @ObjectFreeInstanceCallBack, @ObjectFreeInstanceNext);
ShowMessage(‘在你的 EventLog 窗口里看看有哪些对象被释放了 😃’);
end
else
begin
ShowMessage(‘钩过了’);
end;
end
else
begin
if Assigned(ObjectFreeInstanceNext) then
UnhookProc(@ObjectFreeInstanceNext);
@ObjectFreeInstanceNext := nil;
end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
ShellLink := CreateComObject(CLSID_ShellLink) as IShellLink;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
if Assigned(MessageBoxNext) then
UnhookProc(@MessageBoxNext);
if Assigned(ObjectFreeInstanceNext) then
UnhookProc(@ObjectFreeInstanceNext);
if Assigned(ShellLinkSetPathNext) then
UnhookProc(@ShellLinkSetPathNext);
end;

在这里插入图片描述unit HookUtils;

{ KaTeX parse error: Expected 'EOF', got '}' at position 10: IFDEF FPC}̲ { MODE Delphi}
{$ENDIF}

{.$DEFINE USEINT3} { 在机器指令中插入 INT3,断点指令方便调试 }

interface

function HookProc(ATargetProc, ANewProc: Pointer;
out AOldProc: Pointer): Boolean; overload;
function HookProc(const ATargetModule, ATargetProc: string; ANewProc: Pointer;
out AOldProc: Pointer): Boolean; overload;
function UnHookProc(var AOldProc: Pointer): Boolean;

implementation

{ KaTeX parse error: Expected 'EOF', got '}' at position 13: IFDEF CPUX64}̲ { DEFINE USELONGJMP}
{$ENDIF}

uses
Windows;

const
defAllocMemPageSize = 4096;

type
{ KaTeX parse error: Expected 'EOF', got '}' at position 11: IFNDEF FPC}̲ { IF CompilerVersion < 23}
NativeUInt = LongWord;
{ KaTeX parse error: Expected 'EOF', got '}' at position 6: IFEND}̲ { ENDIF}

TJMPCode = packed record
{ KaTeX parse error: Expected 'EOF', got '}' at position 17: …FDEF USELONGJMP}̲ JMP: Word;…ELSE}
JMP: Byte;
Addr: UINT_PTR;
{$ENDIF}
end;
PJMPCode = ^TJMPCode;

TOldProc = packed record
{ KaTeX parse error: Expected 'EOF', got '}' at position 14: IFDEF USEINT3}̲ Int3OrNop:…ENDIF}
BackCode: array[0…$20 - 1] of Byte;
JmpRealFunc: TJMPCode;

  • 26
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大龙软件研发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值