Delphi: getting the name of the current unit
Posted by jpluimers on 2021/01/28
If you have a class (like TMyClass) in the current unit, you can get the unit name as follows:
- inside a method of the class (either instance or class method): call
UnitNameorUnitScopeto get the unit name - outside a method of the class: call
TMyClass.UnitNameorTMyClass.UnitScope
Delphi added UnitName [WayBack] and Delphi XE2 added UnitScope [Archive.is]. Their implementations are different, but I have not seen classes where the outcome is different.
The code below shows that when the underlying RTTI UnitName field inside a PTypeData referred by a PTypeInfo contains an @ sign, then UnitName takes the part before the @, and UnitScope the part after the @, but I have not yet seen units where the underlying field contains an @ sign.
If you have seen that, please let me know.
I needed this in order to research some unit initialisation order issues.
A post helpful with that was [WayBack] windows – Can I determine the order in which my units have been initialized? – Stack Overflow for which this comment by Ritsaert Hornstra is the most important bit:
Related: If you use a unit in the interface section you know that that unit will be initialized BEFORE the unit that uses that unit. When using units in the implementation section this is not the case. So usually when you are using a unit with a singleton in it, created in it’s initialization section, you should use that unit in the interface section to make sure that it is initialized before use.
There is also an answer [WayBack] by Remko Weijnen that shows how to hack the current initialisation order if you know the address of InitContext inside the System.InitUnits method.
Back to UnitName versus UnitScope, the code:
class function TObject.UnitName: string; var LClassInfo: Pointer; S: _PShortStr; begin LClassInfo := ClassInfo; if LClassInfo <> nil then begin S := @PClassData(PByte(LClassInfo) + 2 + PByte(PByte(LClassInfo) + 1)^).UnitName; if S^[1] <> '@' then Result := UTF8ToString(S^) else Result := UTF8ToString(Copy(S^, Pos(_ShortStr(':'), S^) + 1, MaxInt)); end else Result := ''; end; class function TObject.UnitScope: string; var LClassInfo: Pointer; S: _PShortStr; begin LClassInfo := ClassInfo; if LClassInfo <> nil then begin S := @PClassData(PByte(LClassInfo) + 2 + PByte(PByte(LClassInfo) + 1)^).UnitName; if S^[1] <> '@' then Result := UTF8ToString(S^) else Result := UTF8ToString(Copy(S^, 2, Pos(_ShortStr(':'), S^) - 2)); end else Result := ''; end;
Related:
- [WayBack] delphi – How can I get the unit name of a class if I have the class / VMT address only – Stack Overflow
- [WayBack] delphi – Get Unit Name from InitTable – hack System.pas – Stack Overflow
- [WayBack] How to get unit path in runtime with Delphi? – Stack Overflow
–jeroen






dummzeuch said
“Delphi added UnitName” you probably meant “Delphi 2009 added UnitName”