Reputation: 150
My application dynamically load packages that provide implementation of objects I need. I've got a global function to register object classes on a list structure which I use to create instances dynamically.
procedure RegisterObjectStd(const APackageName, AObjectName: string; const AClass: TClass);
Thereby I can create an instance referenced to a particular context through a factory method
Example:
function CreateObject(const APackageName, AObjectName: string): TObject;
Is there a way to dinamically retrieve the name of the current package (.bpl) in my initialization code?
initialization
RegisterObjectStd(GetCurrentBplName, 'MyObjectName', TMyObjectClass);
Upvotes: 3
Views: 440
Reputation: 612794
Do it using GetPackageInfo
from System.SysUtils
. It's a little involved to call, so here I demonstrate how to wrap it up to obtain the package name:
procedure GetPackageNameInfoProc(const Name: string; NameType: TNameType; Flags: Byte;
Param: Pointer);
begin
if NameType=ntDcpBpiName then begin
PString(Param)^ := Name;
end;
end;
function GetPackageName(Package: HMODULE): string;
var
Flags: Integer;
begin
// Flags should be an out param, but is a var, so this assignment is a little pointless
Flags := 0;
Result := '';
GetPackageInfo(Package, @Result, Flags, GetPackageNameInfoProc);
end;
You can use this on any runtime package loaded in your process. If you want to call it on the package that the code is execiting in, pass HInstance
to GetPackageName
.
Upvotes: 4