Component Object Model

COM allows you to develop independent, reusable software components that connect together to form applications. COM components are binary bits of executable code that function like mini-applications. They can be disconnected and replaced at run time without relinking or recompiling the application. Because COM is language-independent, you can write your components in any programming language you choose.

The benefit of using COM is that you can upgrade application components independently of each other. This allows you to change or add application features without having to upgrade the entire application. You can plug and unplug components from your application, as well as interchange them with other components. In order to achieve this flexibility, COM components must be able to dynamically link together. Dynamic linking is accomplished through encapsulation, the process of hiding the details of how a component is implemented. You can do this by creating an interface through which a component or a client can connect. A client is a program or component that uses another component. All COM components must interact through an interface. Additionally, all COM components must inherit from, and implement, the IUnknown interface.

COM maintains the COM library, which contains a small number of functions and data structures used to create interfaces and coordinate object services. To use the libraries in Windows CE, call the CoInitializeEx function before you call any other functions, except for the memory allocation functions. Similarly, to close the libraries, call the CoUnitialize function.

COM objects operate according to the client/server model. A COM server is a COM object that exports and implements interfaces in order to provide services to clients. The server is responsible for implementing an interface and helping a client navigate among various interfaces. A COM client creates an instance of a server object and retrieves a pointer to it. Clients use the services provided by a server object by calling an interface's methods. A client need not have any knowledge of the implementation details of the server. The underlying COM libraries facilitate communication between a server and a client.

Windows supports two types of COM servers: an in-process (Inproc) server, which resides as a dynamic-link library (DLL) in the client's process, and an out-of-process server, which resides as an executable file (.exe) on either the local or a remote computer. Windows CE supports only in-process servers. A COM server implements the QueryInterface method of the IUnknown interface to make its services available to clients. This method receives an interface identifier (IID) and returns a pointer to that interface if it is implemented within the server component. A client can call methods only on an object that is an instance of the server class. Thus, the client must instantiate the class before it invokes methods of a COM object.