如何使FastCodePatch在Delphi XE2 Win64平台上工作?

前端之家收集整理的这篇文章主要介绍了如何使FastCodePatch在Delphi XE2 Win64平台上工作?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
单元FastCodePatch.pas在Win32平台上工作. Delphi XE2支持Win64平台,任何想法如何使FastCodePatch在Win64平台上工作?
  1. unit FastcodePatch;
  2.  
  3. interface
  4.  
  5. function FastcodeGetAddress(AStub: Pointer): Pointer;
  6. procedure FastcodeAddressPatch(const ASource,ADestination: Pointer);
  7.  
  8. implementation
  9.  
  10. uses
  11. Windows;
  12.  
  13. type
  14. PJump = ^TJump;
  15. TJump = packed record
  16. OpCode: Byte;
  17. Distance: Pointer;
  18. end;
  19.  
  20. function FastcodeGetAddress(AStub: Pointer): Pointer;
  21. begin
  22. if PBYTE(AStub)^ = $E8 then
  23. begin
  24. Inc(Integer(AStub));
  25. Result := Pointer(Integer(AStub) + SizeOf(Pointer) + PInteger(AStub)^);
  26. end
  27. else
  28. Result := nil;
  29. end;
  30.  
  31. procedure FastcodeAddressPatch(const ASource,ADestination: Pointer);
  32. const
  33. Size = SizeOf(TJump);
  34. var
  35. NewJump: PJump;
  36. OldProtect: Cardinal;
  37. begin
  38. if VirtualProtect(ASource,Size,PAGE_EXECUTE_READWRITE,OldProtect) then
  39. begin
  40. NewJump := PJump(ASource);
  41. NewJump.OpCode := $E9;
  42. NewJump.Distance := Pointer(Integer(ADestination) - Integer(ASource) - 5);
  43.  
  44. FlushInstructionCache(GetCurrentProcess,ASource,SizeOf(TJump));
  45. VirtualProtect(ASource,OldProtect,@OldProtect);
  46. end;
  47. end;
  48.  
  49. end.

Ville Krumlinde提供的解决方案不适用于64位软件包.它仅适用于独立的.exe应用程序.

解决方法

对于FastcodeAddressPatch功能,当我尝试时,此版本的工作在32位和64位.关键是将“指针”改为“整数”,因为Intel相对跳转指令($E9)在64位模式下仍然使用32位偏移量.
  1. type
  2. PJump = ^TJump;
  3. TJump = packed record
  4. OpCode: Byte;
  5. Distance: integer;
  6. end;
  7.  
  8. procedure FastcodeAddressPatch(const ASource,OldProtect) then
  9. begin
  10. NewJump := PJump(ASource);
  11. NewJump.OpCode := $E9;
  12. NewJump.Distance := NativeInt(ADestination) - NativeInt(ASource) - Size;
  13.  
  14. FlushInstructionCache(GetCurrentProcess,@OldProtect);
  15. end;
  16. end;
  17.  
  18. procedure Test;
  19. begin
  20. MessageBox(0,'Original','',0);
  21. end;
  22.  
  23. procedure NewTest;
  24. begin
  25. MessageBox(0,'Patched',0);
  26. end;
  27.  
  28. procedure TForm5.FormCreate(Sender: TObject);
  29. begin
  30. FastcodeAddressPatch(@Test,@NewTest);
  31. Test;
  32. end;

我不知道其他功能是什么,但是我猜这应该是这样的:

  1. function FastcodeGetAddress(AStub: Pointer): Pointer;
  2. begin
  3. if PBYTE(AStub)^ = $E8 then
  4. begin
  5. Inc(NativeInt(AStub));
  6. Result := Pointer(NativeInt(AStub) + SizeOf(integer) + PInteger(AStub)^);
  7. end
  8. else
  9. Result := nil;
  10. end;

猜你在找的Delphi相关文章