Page 1 of 1

Hoocking TObjectList.GetItem

Posted: Thu Mar 10, 2005 6:33 pm
by Hechicero
Hi,
I really need help. I want to hook a protected Method. In particular TObjectList.GetItem. I tried many things but I couldnt do it.
Help please!

Esteban Calabria.

Posted: Fri Mar 11, 2005 9:23 am
by madshi
Here's some code about how I'm hooking TListView.UpdateColumn, which also is a protected method:

Code: Select all

var UpdateColumnNextHook : procedure (Self: TObject; index: integer) = nil;

procedure UpdateColumnHookProc(Self: TObject; index: integer);
begin
  if (Self is TDBListView) and (TDBListView(Self).FDB <> nil) then
       TDBListView(Self).HackedUpdateColumn(index)
  else UpdateColumnNextHook(Self, index);
end;

constructor TDBListView.Create(AOwner: TComponent);
begin
  if @UpdateColumnNextHook = nil then
    HookCode(@TDBListView.UpdateColumn, @UpdateColumnHookProc, @UpdateColumnNextHook);
  inherited;
end;
Does that help? Basically you have to add a first "Self: TObject" parameter, because methods always have that (but it's hidden).

Posted: Fri Mar 11, 2005 12:49 pm
by Hechicero
The problem is that the method getItem from TObjectList is protected. If I create a descendent class and hook the super class procedure I get an Unidentified Identifier getItems

This doesnt work for instance. Neither inside a TObjListFriens(TObjectList) or in anyplace outside TObjectList unit

constructor TObjListFriend.Create;
begin
HookCode(@TObjectList.GetItem,@MyGetItem,@f);
end;


I want to be hable to do something like

constructor TObjListFriend.Create;
begin
HookCode(@inherited GetItem,@MyGetItem,@f);
end;

Posted: Fri Mar 11, 2005 2:18 pm
by madshi
This compiles fine for me:

Code: Select all

type
  TObjListFriends = class (TObjectList)
  end;

initialization
  HookCode(@TObjListFriends.GetItem, ...
If you don't overwrite the protected method, then TObjListFriends.GetItem is exactly the method that you want to hook.

Thanks

Posted: Fri Mar 11, 2005 3:40 pm
by Hechicero
Thanks! It seems that works!!!