Search code examples
delphilegacy-codedelphi-3

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

Solution

  • 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