Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent in Delphi 3 of Supports for Interfaces?

I support an application written in Delphi 3 and I would like to put in some improvements to the source code while waiting for the opportunity to upgrade it to a newer version of Delphi. One of the things I would like to use is Interfaces. I know Delphi 3 already has the concept of Interfaces but I am having trouble finding out how to do the equivalent of

if Supports(ObjectInstance, IMyInterface) then
like image 962
Omen Avatar asked Dec 02 '10 10:12

Omen


1 Answers

Write your own implementation of "Supports" function. In Delphi 2009 you can use

function MySupports(const Instance: TObject; const IID: TGUID): Boolean;
var
  Temp: IInterface;
  LUnknown: IUnknown;
begin
  Result:= (Instance <> nil) and
           ((Instance.GetInterface(IUnknown, LUnknown)
             and (LUnknown.QueryInterface(IID, Temp) = 0)) or
            Instance.GetInterface(IID, Temp));
end;

Test:

procedure TForm4.Button3Click(Sender: TObject);
var
  Obj: TInterfacedObject;

begin
  Obj:= TInterfacedObject.Create;
  if MySupports(Obj, IUnknown) then
    ShowMessage('!!');
end;

Hope it will work in Delphi 3

like image 138
kludg Avatar answered Sep 28 '22 04:09

kludg