HRESULT CoRevokeClassObject(dwRegister)
Informs the COM Library that a class factory previously registered with CoRegisterClassObject is no longer available for use. Server applications call this function on shutdown after having detected the necessary unloading conditions.
There are no instances of the class in existence; that is, the object count is zero.
The class factory has a zero number of locks from IClassFactory::LockServer.
The application servicing the class object is not showing itself to the user; that is, not under user control.
When, subsequently, the reference count on the class object reaches zero, the class object can be destroyed, allowing the application to exit.
Argument
Type
Description
dwRegister
DWORD
A token previously returned from CoRegisterclassObject.
Return Value
Meaning
S_OK
Success.
E_UNEXPECTED
An unknown error occurred.
The structure of a server application with its object and class factory is illustrated in Figure 6-2. This figure also illustrates the sequence of calls and events that happen when the client executes the standard object creation sequence of CoGetClassObjectandIClassFactory::CreateInstance.
Figure 6-2: Creation sequence of an object from a server application. Function calls not in COM are from the Windows API.
Compare this figure with .DLL server Figure 6-1 in the previous section. You'll notice that the structure of the server is generally the same that is: both have their object and class factory. You'll also notice that the creation sequence from the client's point of view is identical. Again, once the client determines the CLSID of the desired object that client leaves the specifics up to CoGetClassObject. The only differences between the two figures occur inside the COM Library and the specific means of exposing the class factory from the server (along with the unloading mechanism).
Finally, CoRegisterClassObjectand CoRevokeClassObject along with when a server calls them demonstrate why a reference count on the class factory is insufficient to keep a server in memory and why IClassFactory::LockServer exists. CoRegisterClassObject must, in order to be implemented properly, hold on to the IUnknown pointer passed to it (that is, the class factory). The reference counting rules state that CoRegisterClassObject must call AddRef on that pointer accordingly. This reference count can only be removed inside CoRevokeClassObject.
However, CoRevokeClassObject is only called on application shutdown and not at any other time. How does the server know when to start its shutdown sequence? Since it has to be in the process of shutting down to have the final reference counts on the class factory released through CoRevokeClassObject, it cannot use the reference count to determine when to start the shutdown process in the first place. Therefore there has to be another mechanism through which shutdown is controlled which is IClassFactory::LockServer.