delphidelphi-2007runtime-packages

How to get the dynamic package from which a TPersistentClass comes from?


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?


Solution

  • 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.