Search code examples
delphiguidtypeinfo

How to get interface type info from TGUID?


How can I get the type info from the GUID?

procedure MyProcedure(const InterfaceId: TGuid);
var
  MyTypeInfo: PTypeInfo;
begin
  MyTypeInfo := TypeInfo(InterfaceId);  //E2133 TYPEINFO standard function expects a type identifier
  ...
end;

Solution

  • You have to search through all the RTTI in the EXE. For Delphi 2010 and above:

    unit RTTI.Utilities;
    
    interface
    
    uses System.TypInfo;
    
    function InterfaceTypeInfoOfGUID(const AGUID : TGUID) : PTypeInfo;
    
    implementation
    
    uses System.RTTI;
    
    function InterfaceTypeInfoOfGUID(const AGUID : TGUID) : PTypeInfo;
    
    var
       Context : TRttiContext;
       ItemType : TRttiType;
    
    begin
       for ItemType in Context.GetTypes do
          begin
             if ItemType is TRTTIInterfaceType then
                begin
                   if TRTTIInterfaceType(ItemType).GUID = AGUID then
                      exit(TRTTIInterfaceType(ItemType).Handle);
                end
          end;
       Result := nil;
    end;
    
    end.