Search code examples
delphiinterfacedelphi-2010rtti

delphi: how to transform a TGUID to a PTypeInfo pointer?


I have a TGUID variable, and I want to "transform" it via Rtti to the PTypeInfo that describes that interface..

AGUID := StringToGUID('{19BB9F78-1FB1-4B0F-B691-82EE5CD7A941}');

.. transform AGUID to PTypeInfo ..

AInterface = GlobalContainer.Resolve( <PTypeInfo> expected);

Delphi 2010+


Solution

  • function GetInterfaceTypeInfo(const GUID: TGUID): PTypeInfo;
    var
      Ctx: TRttiContext;
      AType: TRttiType;
    begin
      Result := nil;
      Ctx := TRttiContext.Create;
      try
        for AType in Ctx.GetTypes do
          if (AType.TypeKind = tkInterface) and IsEqualGUID(GetTypeData(AType.Handle)^.Guid, GUID) then
          begin
            Result := AType.Handle;
            Break;
          end;
      finally
        Ctx.Free;
      end;
    end;