If I have an object that implements an interface, it's not too difficult to use RTTI to look up the interface and obtain its GUID. But if I want its name, is there any way to get that? It's simple enough to get a class's name, but for interfaces it seems a bit trickier...
+1
A:
The tricky part is getting RTTI from an interface. If you've managed to do that, then you should already have the name there in the PTypeInfo
record pointer.
In one of my programs, I wrote a special registration system that mapped GUIDs to strings so that I could report failed calls to QueryInterface
and failed "as
" casts. That was in delphi 2005. My understanding is that in later Delphi versions RTTI for interfaces has progressed such that I might not need that system anymore.
Rob Kennedy
2009-08-05 19:44:49
There's RTTI and then there's RTTI. You can get some info from TObject.GetInterfaceTable, including a GUID, but no PTypeInfo there...
Mason Wheeler
2009-08-05 20:32:03
Ah. I don't generally consider stuff that's built into the VMT structure to be RTTI. Terminology aside, you may be stuck with the separate registration I mentioned. I'll check out my old code when I get home later, if there isn't already a better answer posted by then.
Rob Kennedy
2009-08-05 20:37:01
It is my understanding that the "names" of the interfaces are visible in code, but as they are compiled the names are translated to the Guids so that information is somewhat lost. You would have to create a registry much like what Rob suggested to correlate the interface with a string name. Getting the methods implemented by an interface appears possible (see IntfInfo.pas) although I have never tried this.
skamradt
2009-08-05 21:03:58
@Rob: I'm not quite sure what you mean. As near as I can tell, *all* of the RTTI for a class is built into the VMT somewhere. For example, the metadata used to deserialize forms is accessed through the pointers at the offsets vmtTypeInfo, vmtFieldTable, and vmtMethodTable.
Mason Wheeler
2009-08-06 00:47:56
@Rob: Both the name _and the GUID_ are included in the PTypeInfo. If you could somehow enumerate all typeinfo structures you could autogenerate your mapping. Not that this seems any easier than getting the PTypeInfo from an interface (instance), but still. :-)
Paul-Jan
2009-08-06 07:29:44
Sorry, Mason. Maybe I'm making a distinction in my head that doesn't really pan out in real life. I can't really articulate it right now. Anyway, I've looked around for my old code, but I can't find it. Based on Paul-Jan's comment, I must have kept a list and added things with something like `IntfReg.Register(TypeInfo(IFoo))`. It would enforce that only interfaces' typeinfo was added. Then I'd look things up via GUID. I probably parsed the TTypeInfo record into a more friendly record or class with only the fields I was interested in.
Rob Kennedy
2009-08-06 12:38:30
Paul-Jan, I don't think you can enumerate all typeinfo in a program. In fact, I'm pretty sure that the compiler doesn't even bother *generating* certain typeinfo unless you've explicitly asked for it with the `TypeInfo` function somewhere in the program.
Rob Kennedy
2009-08-06 12:41:33