Inspired by this, I have successfully patched a strict private (!) function in Delphi 32bits using the Delphi Detours Library and following code:
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);
Whilst this works fine in Win32, it fails in Win64. The interception works but statement Result := Trampoline_TFormStyleHook_GetBorderSize(Self)
returns trash. I guess this is because function (Self: TFormStyleHook) : TRect
is not equivalent to function: TRect of object
in Win64. Does anyone have an idea about how to make the above work in Win64. I am using Delphi Rio, but it works the same with Delphi Tokyo.