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?
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 callsLoadLibraryEx()
to load the COM server DLL if necessary. But how long is the DLL kept loaded? In fact COM callsDllCanUnloadNow()
for every loaded COM server DLL periodically. If it returnsS_OK
COM is allowed to callFreeLibrary()
.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 (usuallyCComObject
constructor) and increments the variable, when an object is destroyed -CComModule::Unlock()
is called from its destructor and decrements the variable. WhenCComModule::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.