Basic COM   «Prev  Next»
Lesson 8 In-process COM servers DllCanUnloadNow
Objective Describe what DllCanUnloadNow does.

DllCanUnloadNow Description

Understanding DllCanUnloadNow builds on the reference-counting concepts covered in the previous module and on the information on In-process servers as discussed earlier in this module.

Local/remote transparency

COM's local/remote transparency isolates the location and activation of a server from the client. A client simply asks for a class factory and, using IClassFactory::CreateInstance, creates instances of a COM object. Recall that COM uses reference counting to manage the lifetime of a COM object. When a COM object, via QueryInterface, "gives away" an interface pointer (i.e., assigns the output pointer parameter to an interface), it increments a reference counter by calling AddRef. The COM client calls Release when it is finished using an interface pointer. Release decrements the reference counter.

Reference counting

Reference counting supports lifetime management of the COM object. A COM server uses reference counts to track active (COM) objects. A server can be unloaded from memory when all its reference counts are 0.
In-process servers provide function DllCanUnloadNow to assist with the unloading process. Periodically, COM will call into DllCanUnloadNow to ask the server if it can be removed from memory.
In DllCanUnloadNow, a server checks all its reference counts and server locks (i.e., set via IClassFactory::LockServer). If they are all 0, no COM objects within the server are in use. The server returns TRUE. Upon seeing a non-zero return, COM will unload the in-process server. If any of the reference counts are non-zero or the lock counter is non-zero, the server returns FALSE and COM leaves the server in memory.

dllCanUnloadNow - Exercise

Click the Exercise link below to apply what you have learned about DllCanUnloadNow.
dllCanUnloadNow - Exercise