我想修补例程调用,以便能够通过一些修改自己处理它。 我正在编写一个资源加载器。我想修补 Delphi 的 LoadResourceModule 和 InitInheritedComponent 例程与我的例程相同。我已经检查了 MadExcept.pas 单元中的 PatchAPI 调用,但无法弄清楚是否可以将其用于我的项目。
我想要类似的东西
my exe at runtime calls -> LoadResourceModule -> jump to -> MyCustomResourceModule...
对此的任何指示都会非常有帮助。
最佳答案
我使用以下代码:
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
来实现您的hook/patch/detour:
RedirectProcedure(@LoadResourceModule, @MyLoadResourceModule);
这适用于 32 位代码。如果新旧函数都驻留在同一个可执行模块中,它也适用于 64 位代码。否则跳转距离可能会超出32位整数的范围。
如果有人能够提供适用于 64 位地址空间的替代方案,无论两个地址相距多远,我都会非常感兴趣。
关于delphi - delphi 中的补丁例程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8978177/