Search code examples
com

What is the point of IClassFactory::LockServer?


Why does IClassFactory::LockServer exist when it seems to me that IClassFactory::AddRef / IClassFactory::Release can accomplish the same goal?


Solution

  • This is explained in detail in Don Box's book Essential COM.

    AddRef/Release on IClassFactory interfaces of class objects are often empty methods in out-of-process COM servers. This is because an internal reference to the class object is maintained by the server when it calls CoRegisterClassObject, and thus the "normal" in-process server implementation of AddRef/Release would result in the reference count on the class object always exceeding one, and the server would not know when to call CoRevokeClassObject.

    The COM runtime calls IClassFactory::LockServer when it marshals an external reference to a class object following a call to CoGetClassObject. In this way the server process lifetime can be properly controlled based on the existence or otherwise of external references.