Exposing the Class Factory from .EXE Servers

To expose a class factory from a server application is a different matter than for a .DLL server for the reason that the application executes in a different process from the client. Thus, the COM Library cannot just obtain a pointer to an exported function and call that function to retrieve the class factory.

When COM launches an application from within CoGetClassObject it must wait for that application to register a class factory for the desired CLSID through the function CoRegisterClassObject.6. Once that class factory appears to COM it can return an interface pointer (actually a pointer to the proxy) to the client. CoGetClassObject may time out if the server application takes too long.

The server can differentiate between times it is launched stand-alone and when it is launched from within COM. When COM launches the application it includes a switch "/Embedding"7. on the server's command line. If the flag is present, the server must register its class factory with CoRegisterClassObject. If the flag is absent, the server may or may not choose to register depending on the object class.

Note that a server application can support any number of object classes by calling CoRegisterClassObject on startup. In fact, a server must register all supported class factories because the application is not told which CLSID was requested in the client.

Where CoRegisterClassObject registers a servers factories with COM on startup, the function CoRevokeClassObject un-registers those same factories on application shutdown so they are no longer available, meaning COM must launch the server again for those class factories. Each call to CoRegisterClassObject must be matched with a call to CoRevokeClassObject.