1

My application is built using runtime packages and loads them by using the LoadPackage function. Then it uses the GetClass function to obtain class types.

var
  MyClass : TPersistentClass;
begin
  if(LoadPackage('.\PackageA.bpl') = 0) then
    raise Exception.Create('Error loading PackageA.bpl');
  if(LoadPackage('.\PackageB.bpl') = 0) then
    raise Exception.Create('Error loading PackageB.bpl');

  MyClass := GetClass('TMyClass');
end;

Is there any way to get the name of the package from which MyClass comes from?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • On modern Delphi versions TPresistentClass has `UnitName` method available, but I'm afraid that in older versions you need to descend your classes from TObject at least to have Access to such method. – SilverWarior Mar 15 '19 at 12:55

1 Answers1

3

Use the RTL's FindClassHInstance() function to get the handle of the loaded package that owns the class type that GetClass() returns. This will be the same handle that LoadPackage() returns.

You can either track the loaded package handles yourself, or you can pass the handle to the Win32 API GetModuleFileName() function to query the handle for its package's path and filename.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770