gpt4 book ai didi

delphi - Delphi Win64 中的 Monkey 修补方法(函数)

转载 作者:行者123 更新时间:2023-12-02 01:20:12 26 4
gpt4 key购买 nike

灵感来自this ,我已经使用 Delphi Detours Library 成功修补了 Delphi 32 位中的严格私有(private) (!) 函数。和以下代码:

var
Trampoline_TFormStyleHook_GetBorderSize : function (Self: TFormStyleHook) : TRect;

type
TFormStyleHookFix = class helper for TFormStyleHook
function GetBorderSizeAddr: Pointer;
end;

function TFormStyleHookFix.GetBorderSizeAddr: Pointer;
var
MethodPtr: function: TRect of object;
begin
with Self do MethodPtr := GetBorderSize;
Result := TMethod(MethodPtr).Code;
end;

function Detour_TFormStyleHook_GetBorderSize(Self: TFormStyleHook): TRect;
begin
Result := Trampoline_TFormStyleHook_GetBorderSize(Self);
if (Screen.PixelsPerInch > 96) then
Result.Top := MulDiv(Result.Top, 96, Screen.PixelsPerInch);
end;

initialization
Trampoline_TFormStyleHook_GetBorderSize :=
InterceptCreate(TFormStyleHook(nil).GetBorderSizeAddr,
@Detour_TFormStyleHook_GetBorderSize)
finalization
InterceptRemove(@Trampoline_TFormStyleHook_GetBorderSize);

虽然这在 Win32 中工作正常,但在 Win64 中却失败了。拦截有效,但语句 Result := Trampoline_TFormStyleHook_GetBorderSize(Self) 返回垃圾。我猜这是因为 function (Self: TFormStyleHook) : TRect 不等于 Win64 中的 function: TRect of object。有谁知道如何在 Win64 中实现上述工作。我使用的是 Delphi Rio,但它与 Delphi Tokyo 的工作原理相同。

最佳答案

没关系。我找到了答案。以下内容适用于 win32 和 win64。怀疑 function (Self: TFormStyleHook) : TRect 与 Win64 中的 function: TRect of object 不等效。您需要将 Trampoline 函数声明为 function: TRect of object 并使用强制转换为 TMethod 来设置/获取代码指针。

  type
TGetBorderSize = function: TRect of object;

TFormStyleHookFix = class helper for TFormStyleHook
function GetBorderSizeAddr: Pointer;
function Detour_GetBorderSize: TRect;
end;

var
Trampoline_TFormStyleHook_GetBorderSize : TGetBorderSize;
Detour_TFormStyleHook_GetBorderSize : TGetBorderSize;

function TFormStyleHookFix.GetBorderSizeAddr: Pointer;
var
MethodPtr: TGetBorderSize;
begin
with Self do MethodPtr := GetBorderSize;
Result := TMethod(MethodPtr).Code;
end;

function TFormStyleHookFix.Detour_GetBorderSize: TRect;
var
MethodPtr: TGetBorderSize;
begin
TMethod(MethodPtr).Code := TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code;
TMethod(MethodPtr).Data := Pointer(Self);
Result := MethodPtr;
if (Screen.PixelsPerInch > 96) then
Result.Top := MulDiv(Result.Top, 96, Screen.PixelsPerInch);
end;

initialization
Detour_TFormStyleHook_GetBorderSize := TFormStyleHook(nil).Detour_GetBorderSize;
TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code :=
InterceptCreate(TFormStyleHook(nil).GetBorderSizeAddr,
TMethod(Detour_TFormStyleHook_GetBorderSize).Code)
finalization
InterceptRemove(TMethod(Trampoline_TFormStyleHook_GetBorderSize).Code);

关于delphi - Delphi Win64 中的 Monkey 修补方法(函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53511873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com