问题
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