以下是MSDN中关于LoadLibrary的描述:
During initial process startup or after a call to LoadLibrary, the system scans the list of loaded DLLs for the process. For each DLL that has not already been called with the DLL_PROCESS_ATTACH value, the system calls the DLL's entry-point function. This call is made in the context of the thread that caused the process address space to change, such as the primary thread of the process or the thread that called LoadLibrary.
所以我认为线程可以调LoadLibrary和FreeLibrary.
如果不行,我的意见是用一个全局变量,记录使用DLL的线程个数,每个线程一开始都增加之,每个线程在结束时减少之,并判断如果是0,则FreeLibrary.当然此全局变量要采取同步措施,但很简单,用InterlockedIncrement/InterlockedDecrement就行.
下面引自MSDN:
Each process maintains a reference count for each loaded library module. This reference count is incremented each time LoadLibrary is called and is decremented each time FreeLibrary is called. A DLL module loaded at process initialization due to load-time dynamic linking has a reference count of one. This count is incremented if the same module is loaded by a call to LoadLibrary.
所以你的每个线程都各自调一遍LoadLibrary和FreeLibrary即可,互不干扰