As I read here,
the VMT also contains a number of “magic” fields to support features such as parent class link, instance size, class name, dynamic method table, published methods table, published fields table, RTTI table, initialization table for magic fields, the deprecated OLE Automation dispatch table and implemented interfaces table
It looks like the VMT does not include a field which contains the unit name where the class is defined. Is there some 'compiler magic' involved?
I cannot see why the VMT should be involved here. TObject already exposes a class function UnitName
for that.
System.TObject.UnitName
VMT includes a pointer to class RTTI (provided by ClassInfo
method); class RTTI includes a class unit name. As an exercise you can get unit name from VMT pointer, I have written this (tested on Delphi XE):
uses TypInfo;
type
TObj = class
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Obj: TObj; // dummy obj instance
VMT: Pointer;
P: Pointer; // class info
begin
// you can get VMT pointer so
Obj:= TObj.Create;
VMT:= PPointer(Obj)^;
Obj.Free;
// or so
VMT:= Pointer(TObj);
P:= PPointer(PByte(VMT) + vmtTypeInfo)^;
if P <> nil then
ShowMessage(GetTypeData(P).UnitName);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With