, DLL . , DLL . , DLL , , DLL . , DLL, API- CoFreeUnusedLibraries:
void CoFreeUnusedLibraries(void);
. CoFreeUnusedLibraries DLL, , . DLL DllCanUnloadNow, .
DllCanUnloadNow, DLL , :
HRESULT DllCanUnloadNow(void);
DLL , S_OK. DLL , S_FALSE. DLL , . , DLL . , DLL (lock count) :
LONG g_cLocks = 0; void LockModule(void) { InterlockedIncrement(&g_cLocks); } void UnlockModule(void) { InterlockedDecrement(&g_cLocks); }
DllCanUnloadNow :
STDAPI DllCanUnloadNow(void) { return g_cLocks == 0 ? S_OK : S_FALSE; }
O LockModule UnlockModule.
, DLL : , IClassFactory::LockServer. , DllCanUnloadNow . , ( , ) AddRef:
STDMETHODIMP_(ULONG) Chimp::AddRef(void) { if (m_cRef == 0) LockModule(); return InterlockedIncrement(&m_cRef); }
Release:
STDMETHODIMP_(ULONG) Chimp::Release (void) { LONG res = InterlockedDecrement(&m_cRef); if (res == 0) { delete this; UnlockModule(); } return res; }
, ( , ), , AddRef Release :
STDMETHODIMP_(ULONG) ChimpClass::AddRef(void) { LockModule(); return 2; } STDMETHODIMP_(ULONG) ChimpClass::Release (void) { UnlockModule(); return 1; }
, IClassFactory, IClassFactory::LockServer:
STDMETHODIMP ChimpClass::LockServer(BOOL bLock) { if (bLock) LockModule(); else UnlockModule(); return S_OK; }
6, IClassFactory::LockServer , .
, CoFreeUnusedLibraries/DllCanUnloadNow (race condition). , , DLL, CoFreeUnusedLibraries. , . , Windows NT 4.0 Service Pack 2 . Service Pack 2 , DLL , , DLL CoFreeUnusedLibraries, DLL DLL, . , DLL CoFreeUnusedLibraries, , Release 1. , DLL , .
1 , Windows NT 5.0 , DLL . SDK.