-
Notifications
You must be signed in to change notification settings - Fork 14
/
D7ComboBoxStringsGetPatch.pas
96 lines (81 loc) · 2.33 KB
/
D7ComboBoxStringsGetPatch.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
unit D7ComboBoxStringsGetPatch;
// The patch fixes TCustomComboBoxStrings.Get method for empty string item in Delphi 7.
{$I rpconf.inc}
interface
implementation
{$IFDEF ISDELPHI7}
uses
Windows, SysUtils, StdCtrls;
resourcestring
RsPatchingFailed = 'TCustomComboBoxStrings.Get patching failed.';
type
TPatchResult = (prNotNeeded, prOk, prError);
function PatchCode(RoutineStartAddr: Pointer; PatchOffset: Cardinal; OriginalCode: Pointer;
OriginalCodeLen: Cardinal; PatchedCode: Pointer; PatchedCodeLen: Cardinal): TPatchResult;
const
JmpOpCode = $25FF;
type
PPackageThunk = ^TPackageThunk;
TPackageThunk = packed record
JmpInstruction: Word;
JmpAddress: PPointer;
end;
var
CodeStart: Pointer;
BytesWritten: DWORD;
begin
if FindClassHInstance(System.TObject) <> HInstance then
with PPackageThunk(RoutineStartAddr)^ do
if JmpInstruction = JmpOpCode then
RoutineStartAddr := JmpAddress^
else
begin
Result := prError;
Exit;
end;
CodeStart := Pointer(LongWord(RoutineStartAddr) + PatchOffset);
if CompareMem(CodeStart, OriginalCode, OriginalCodeLen) then
begin
if WriteProcessMemory(GetCurrentProcess, CodeStart, PatchedCode, PatchedCodeLen, BytesWritten) and
(BytesWritten = PatchedCodeLen) then
begin
FlushInstructionCache(GetCurrentProcess, CodeStart, PatchedCodeLen);
Result := prOk;
end
else
Result := prError;
end
else
Result := prNotNeeded;
end;
type
TCustomComboBoxStringsHack = class(TCustomComboBoxStrings);
function AddrOfTCustomComboBoxStringsGet: Pointer;
begin
Result := @TCustomComboBoxStringsHack.Get;
end;
procedure PatchTCustomComboBoxStringsGet;
const
OriginalCode: Cardinal = $74FFF883; // CMP EAX, -1 | JZ +$26
PatchedCode: Cardinal = $7E00F883; // CMP EAX, 0 | JLE +$26
PatchOffset = $1F;
var
PatchResult: TPatchResult;
begin
PatchResult := PatchCode(AddrOfTCustomComboBoxStringsGet, PatchOffset, @OriginalCode, SizeOf(OriginalCode),
@PatchedCode, SizeOf(PatchedCode));
case PatchResult of
prError:
begin
if IsConsole then
WriteLn(ErrOutput, RsPatchingFailed)
else
MessageBox(0, PChar(RsPatchingFailed), nil, MB_OK or MB_ICONSTOP or MB_TASKMODAL);
RunError(1);
end;
end;
end;
initialization
PatchTCustomComboBoxStringsGet;
{$ENDIF}
end.