How to detect UnloadPackage from the target BPL?

☆樱花仙子☆ 提交于 2019-12-13 03:49:58

问题


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:


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;


来源:https://stackoverflow.com/questions/56785458/how-to-detect-unloadpackage-from-the-target-bpl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!