The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

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 UnitName or UnitScope to get the unit name
  • outside a method of the class: call TMyClass.UnitName or TMyClass.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:

–jeroen

One Response to “Delphi: getting the name of the current unit”

  1. dummzeuch said

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

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.