delphi中的补丁例程调用 [英] Patch routine call in delphi

查看:17
本文介绍了delphi中的补丁例程调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修补一个例行调用,以便能够通过一些修改自己处理它.我正在编写一个资源加载器.我想修补 Delphi 的 LoadResourceModule 和InitInheritedComponent 例程和我的一样.我已经检查了 MadExcept.pas 单元中的 PatchAPI 调用,但无法弄清楚我是否可以在我的项目中使用它.

I want to patch a routine call to be able to handle it myself with some modifications. I am writing a resource loader. I want to patch the Delphi's LoadResourceModule and InitInheritedComponent routines with that of mine. I have checked PatchAPI call in MadExcept.pas unit, but couldn't figure it out if i can use that for my project.

我想要类似的东西

我的 exe 在运行时调用 -> LoadResourceModule -> 跳转到 -> MyCustomResourceModule...

my exe at runtime calls -> LoadResourceModule -> jump to -> MyCustomResourceModule...

任何关于此的指示都会非常有帮助.

Any pointers on this would be very helpful.

推荐答案

我使用如下代码:

procedure PatchCode(Address: Pointer; const NewCode; Size: Integer);
var
  OldProtect: DWORD;
begin
  if VirtualProtect(Address, Size, PAGE_EXECUTE_READWRITE, OldProtect) then 
  begin
    Move(NewCode, Address^, Size);
    FlushInstructionCache(GetCurrentProcess, Address, Size);
    VirtualProtect(Address, Size, OldProtect, @OldProtect);
  end;
end;

type
  PInstruction = ^TInstruction;
  TInstruction = packed record
    Opcode: Byte;
    Offset: Integer;
  end;

procedure RedirectProcedure(OldAddress, NewAddress: Pointer);
var
  NewCode: TInstruction;
begin
  NewCode.Opcode := $E9;//jump relative
  NewCode.Offset := NativeInt(NewAddress)-NativeInt(OldAddress)-SizeOf(NewCode);
  PatchCode(OldAddress, NewCode, SizeOf(NewCode));
end;

您将通过调用 RedirectProcedure 来实现您的钩子/补丁/绕行:

You would implement your hook/patch/detour by calling RedirectProcedure:

RedirectProcedure(@LoadResourceModule, @MyLoadResourceModule);

这适用于 32 位代码.如果旧函数和新函数都驻留在同一个可执行模块中,它也适用于 64 位代码.否则跳转距离可能会超出32位整数范围.

This will work for 32 bit code. It will also work for 64 bit code provided that both the old and new functions reside in the same executable module. Otherwise the jump distance may exceed the range of a 32 bit integer.

如果有人可以提供适用于 64 位地址空间的替代方案,无论两个地址相距多远,我都会非常感兴趣.

I'd be very interested if somebody could provide an alternative that worked for 64 bit address space irrespective of how far apart the two addresses were.

这篇关于delphi中的补丁例程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆