Skip to content

Instantly share code, notes, and snippets.

@randydom
Forked from superswanman/RioGetItFix.pas
Created November 28, 2018 06:37
Show Gist options
  • Save randydom/03f165cb28a2a44d29b51fb35e822f05 to your computer and use it in GitHub Desktop.
Save randydom/03f165cb28a2a44d29b51fb35e822f05 to your computer and use it in GitHub Desktop.
unit RioGetItFix;
interface
procedure Register;
implementation
uses
Winapi.Windows, System.SysUtils, Vcl.Themes;
const
PackageName = 'GetIt260.bpl';
PatchOffset = $8AB14;
OriginalCodes: array[0..4] of Byte = ($8B, $45, $F4, $8B, $10);
var
IsHooked: Boolean;
ReturnAddr1: Pointer;
ReturnAddr2: Pointer;
function GetStyleServices: TCustomStyleServices;
begin
Result := TStyleManager.ActiveStyle;
end;
procedure Trampoline;
asm
MOV EAX,DWORD PTR [EBP-$C]
TEST EAX,EAX
JE @@1
MOV EDX,DWORD PTR [EAX]
JMP [ReturnAddr1]
@@1:
CALL GetStyleServices
JMP [ReturnAddr2]
end;
procedure Patch(Address: Pointer; const Data: array of Byte);
var
oldProtect: DWORD;
begin
VirtualProtect(Address, Length(Data), PAGE_READWRITE, oldProtect);
Move(Data[0], Address^, Length(Data));
VirtualProtect(Address, Length(Data), oldProtect, nil);
FlushInstructionCache(GetCurrentProcess, Address, Length(Data));
end;
procedure Register;
var
hModule: THandle;
patchAddr: Pointer;
jumpOffset: NativeInt;
bytes: TBytes;
begin
hModule := GetModuleHandle(PackageName);
if hModule = 0 then Exit;
patchAddr := Pointer(hModule + PatchOffset);
if not CompareMem(patchAddr, @OriginalCodes[0], SizeOf(OriginalCodes)) then Exit;
ReturnAddr1 := Pointer(hModule + PatchOffset + SizeOf(OriginalCodes));
ReturnAddr2 := Pointer(hModule + PatchOffset + SizeOf(OriginalCodes) + 6);
jumpOffset := NativeInt(@Trampoline) - NativeInt(ReturnAddr1);
bytes := [$E9] + BytesOf(@jumpOffset, SizeOf(jumpOffset));
Patch(patchAddr, bytes);
IsHooked := True;
end;
procedure Unregister;
var
hModule: THandle;
begin
if not IsHooked then Exit;
hModule := GetModuleHandle(PackageName);
if hModule = 0 then Exit;
Patch(Pointer(hModule + PatchOffset), OriginalCodes);
end;
initialization
finalization
Unregister;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment