以下是我在實作動態載入的 package 時所遇到的問題,以及在 Borland 
新聞群組上,Peter Below (TeamB) 所做的回答,問題的大意是關於呼叫
兩次以上的 LoadPackage 載入同一個模組所產生的相關問題:
===============================================================
> Dear all:
>   I'm cutting my database application into several
> dynamically loaded packages (using Delphi5) ,
> I use the following code to load a package:
> 
>   ModuleInstance := LoadPackage('Package1.bpl');
> 
> The UnloadAddinPackage procedure is too long,
> so I skip it, I use a 'standard' way which copied from
> others' example.
> The Package1 contains Form1 that will be registered
> in Unit1's Initialization block, everything is ok, but I
> found if I load the package twice, some problem
> happened, below is the detail steps:
>
> 1. Load Package1.bpl
> 2. Load Package1.bpl again
> 3. Show Form1 ==> Ok!
> 4. UnLoad Package1.bpl
> 5. Load Package1.bpl
> 6. Show Form1 ==> Wrong! Because Form1 class not registered.
>
> Do I need Unload the package twice, too?
> Or anyway to prevent loading the same package?
>
> Michael

So don't do it, the second load request will not load the package 
again, instead you get the module handle of the already loaded instance 
back. And the startup code of the module is not executed again. The 
load count of the module is incremented, though, so if you unload it 
once you just decrement the load count, but also execute the cleanup 
code that unregisters the form class (that is triggered directly by 
UnloadPackage). 

You can test if a module is loaded with 

   If GetModulehandle( modulename ) <> 0 Then
     ...module is loaded. 
     
The module name is simply the filename minus extension in most cases. 
Or you can simply try to create the form via getClass and load the 
package if that fails. 


Peter Below (TeamB)  100113.1101@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Sent using Virtual Access 5.00 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe
===============================================================
由於重複呼叫 LoadPackage 載入同一個模組時只會遞增該模組的參考計
數,所以只呼叫一次 UnloadPackage 也只會遞減參考計數而沒有真正釋
放該模組,這種行為與 WinAPI 的 FreeLibrary 相同(事實上,
UnloadPackage 呼叫了 FreeLibrary 來釋放模組)。
最後我採取呼叫 GetModuleHandle 判斷模組是否已經載入的方式來解決
這個問題,GetModuleHandle 傳入的參數是模組的檔名,不可以包含路徑
名稱,但是要包含副檔名,例如:
  if GetModuleHandle('Package1.bpl') = 0 then
    ...
此外,我也用一個 StringList 來自行維護模組串列,以利除錯之用。

    Source: geocities.com/huanlin_tsai/faq

               ( geocities.com/huanlin_tsai)