修补程序调用delphi [英] Patch routine call in delphi

查看:297
本文介绍了修补程序调用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.

我想要像


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

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

任何指针都将非常有用。

Any pointers on this would be very helpful.

推荐答案

我使用以下代码: / p>

I use the following code:

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

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天全站免登陆