Hi,

I am using a TInterfaceList to store some interfaces and am having some
interesting problems. I have a solution to the problem but I don't understand
why it works this way.

When I add an interface to the the TInterfaceList and I typecast it to an
IUnknown (the Add method expects an IUnknown) then everything works fine. If I
don't typecast it and just let it get automatically converted, then the address
of the added interface stored in the TList inside the TInterfaceList is
different to if I do typecast it. This affects the IndexOf method of the
TInterfaceList:


function TList.IndexOf(Item: Pointer): Integer;
begin
  Result := 0;
  while (Result < FCount) and (FList^[Result] <> Item) do
    Inc(Result);
  if Result = FCount then
    Result := -1;
end;



In my code I am adding a new interface to the list and then calling a routine
that loops through the list to update a list box. The update procedure checks
the item index of each item and this is where the interesting stuff starts
happening. I'll use an example interface to explain (without casting the
interface as a IUnknown when added to the TInterfaceList):

The update routine passes the interface to the TInterfaceList.IndexOf method -
the interface address is $F04FB4 as returned by the TInterfaceList. The
TList.IndexOf method receives it as a pointer whose address is $F04FCC. The
Address of the FList^[Result] (where result is the position of the item) is
$F04FF4! Typecasting Pointer(FList^[Result]) it is still $F04FCC. Typecasting
IUnknown(Pointer(FList^[4])) yields the address $F0FB4!!!!!!! It's the same
item, but the TList.IndexOf function returns -1 because the pointer it got
passed was $F04FCC!



As I said, casting the interface to an IUnknown removes the problem and I am
guessing that the problem lies in there being a difference between allowing the
interface to be received as an IUnknown (in the Add method) as opposed to
passing it as an IUnknown (to the add method).



Can anyone shed light on this for me?



Thanks,



Phil.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to