Developers Workshop to COM and ATL 3.0
| < Free Open Study > |
|
This chapter will examine ATL's support for component housing in far greater detail than Chapter 6. During the initial chapters of this book you were given the chance to build in-process and local servers from the ground up. Although COM-based DLL and EXE servers are implemented in slightly different ways, each is responsible for providing the following services:
-
A COM server must provide a mechanism to expose class factories to the COM runtime. DLL servers expose their class factories using DllGetClassObject(). EXE servers post class factories to the class table using CoRegisterClassObject().
-
A COM server must manage its lifetime. DLL servers maintain a lock counter (e.g., active objects + server locks), which can be obtained using DllCanUnloadNow(). If the lock count is at zero, SCM may unload the server from memory (as well, a client can invoke DllCanUnloadNow() indirectly using CoFreeUnusedLibraries()). EXE servers also maintain a server-wide lock counter; however, they are responsible for shutting themselves down when the count has reached zero. In a typical implementation, WinMain() establishes a dummy message loop to keep the server in memory that will fail upon receiving a WM_QUIT message sent when the lock count has transitioned to zero.
-
A well-behaved COM server should register and unregister essential information into (or out of) the system registry when asked to do so. COM is very dependent upon the registry, and therefore a proper server should be able to insert (or delete) ProgIDs, AppIDs, CLSIDs, LIBIDs, and IIDs upon demand. DLL servers export two well- known functions to support self-registration: DllRegisterServer() and DllUnregister- Server(). EXE servers provide the same functionality as these DLL export functions by testing for the -regserver or -unregserver command line parameters.
While coding your component housing "by hand" is not impossible by any means, it can be a chore to reinvent the wheel time and time again. Using the ATL COM AppWizard, you can receive component housing with the click (or two) of a button. Regardless of how you choose to package your COM objects (DLL or EXE), CComModule and the server's object map provide the core support for your server's housing details. Our first task, then, is to review the basics of CComModule.
| < Free Open Study > |
|