I've been trying 开发者_运维技巧to determine what this function does, however I cannot seem to find it anywhere under the MSDN documentation of the CComModule class.
Could anyone tell me what it is used for?
This function is for DllCanUnloadNow()
to work properly.
You know that when you call CoCreateInstance()
for an in-proc server COM automagically calls LoadLibraryEx()
to load the COM server DLL if necessary. But how long is the DLL kept loaded? In fact COM calls DllCanUnloadNow()
for every loaded COM server DLL periodically. If it returns S_OK
COM is allowed to call FreeLibrary()
.
When is it safe to unload the DLL? Obviously you can't unload it until all the objects implemented by the DLL are destroyed. So here comes "lock count" - an global integer variable counting the number of live objects implemented by the DLL.
When a new COM object is created - CComModule::Lock()
is called from its constructor (usually CComObject
constructor) and increments the variable, when an object is destroyed - CComModule::Unlock()
is called from its destructor and decrements the variable. When CComModule::GetLockCount()
returns zero it means that there no live objects and it's safe to unload the DLL.
So the lock count is very similar to the reference count implemented by IUnknown
. The reference count is per object, the lock count is per COM in-proc server.
精彩评论