1
votes

Is there a part of code which is executed when a dynamic package is unloaded calling UnloadPackage function?

var
  MyPackageHandle : THandle;
begin
  MyPackageHandle := LoadPackage('.\MyPackage.bpl');
  if(MyPackageHandle <> 0) then 
    UnloadPackage(MyPackageHandle);
end;

In this case, I need to execute some code inside MyPackage.bpl when it's unloaded

1
Do initialization and finalization work in this case maybe?nil
Wouldn't it be better to fix the bug properly?David Heffernan

1 Answers

0
votes

The general rule is that you should put code that needs to be called when your package is unloaded into the finalization part of your unit. I know from your other package that you're trying to unload a dll. But the catch is that should never load/unload a dll from initialization or finalization.

So what you need to do is have a function in your package that you will call from your main application, that performs the clean-up.

type
  TCleanup = procedure;
var
  MyPackageHandle : THandle;
  CleanupProc: TCleanup;
begin
  MyPackageHandle := LoadPackage('.\MyPackage.bpl');
  if(MyPackageHandle <> 0) then
  begin
    @CleanupProc := GetProcAddress(MyPackageHandle, 'Cleanup' );
    if @CleanupProc <> nil then
      CleanupProc;
    UnloadPackage(MyPackageHandle);
  end;
end;