Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to patch private function and private method

405 views
Skip to first unread message

Chau Chee Yang

unread,
Dec 25, 2007, 10:38:06 PM12/25/07
to
Hi,

I learned a technique recently that we can patch the Delphi VCL source
without changing the original source code. This is great as we can
fixed bugs we faced.

I can do the followings:

1. Patch public function: I can use FastCodePatch.pas. FastCodePatch
doesn't seems to work if the functions is declared in package.

2. Patch public methods in class: I can use CodeRedirect. It works even
class is declared in package.

But how to patch the folowings:

1. Patch private function (or in package)
2. Patch private or protected methods


--
Best regards,
Chau Chee Yang

E Stream Software Sdn Bhd
URL: www.sql.com.my
SQL Financial Accounting

Andreas Hausladen

unread,
Dec 26, 2007, 3:08:39 AM12/26/07
to
Chau Chee Yang wrote:

> 1. Patch private function (or in package)
> 2. Patch private or protected methods

- Private methods in packages are easy. They are exported and you can
use GetProcAddress to get their addresses.

- Protected functions can be patched by using a cracker class:

type
TListCracker = class(TList);

CodeRedirect(@TListCracker.Get, ...);


- Private methods and functions that aren't in a package are not that
easy to patch. You must first find a function/method that calls the
private function/method. The simpler that function/method is, the
easier you can find the relative calling address by inspecting the CPU
view.

Here is an example to get the System.GetDynaMethod function address:

function GetAddrGetDynaMethod: Pointer;
var
P: PByteArray;
begin
P := GetActualAddr(GetCallDynaInstAddr);
while P[0] <> $E9 do
begin
if (P[0] = $E8) and (P[2] = $FF) and (P[3] = $FF) and (P[4] = $FF)
then
begin
Result := Pointer(Integer(@P[5]) + PInteger(@P[1])^);
Exit;
end;
Inc(PByte(P));
end;
Result := nil;
end;

--
Regards,

Andreas Hausladen

Chau Chee Yang

unread,
Dec 26, 2007, 11:32:27 PM12/26/07
to
I found some bugs in unit FmtBcd.pas. If I want to patch this method:

procedure MoveNibblesToRight(var BcdVal: TBcd; BcdIn: TBcd);

This procedure is private. How to patch this function in the following
2 situations:

1. compile my app as single EXE.

2. Compile my app with runtime package. FmtBcd.pas is keep in
'dbrtl100.bpl' package.

Andreas Hausladen

unread,
Dec 27, 2007, 3:44:00 AM12/27/07
to
Chau Chee Yang wrote:

> I found some bugs in unit FmtBcd.pas. If I want to patch this method:

The easiest way would be to patch BcdAdd() and NormalizeBcd() which are
both public and are the only functions that call MoveNibblesToRight
directly or indirectly.

--
Regards,

Andreas Hausladen

Andreas Hausladen

unread,
Dec 27, 2007, 4:20:39 AM12/27/07
to
Andreas Hausladen wrote:

> The easiest way would be to patch BcdAdd() and NormalizeBcd() which
> are both public and are the only functions that call
> MoveNibblesToRight directly or indirectly.

In other words: You copy the code for BcdAdd and NormalizeBcd and all
thier private dependencies to the patching unit and then you fix the
bug in MoveNibblesToRight there.

--
Regards,

Andreas Hausladen

0 new messages