Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
562 Project 5 CREATING A COM COMPONENT USING ATL
In this architecture, the client and the server processes can either coexist on the same computer or, in a network, can exist on different computers. When these two processes exist on different computers, the data is passed to and fro, which implies that data accepted by the client process is passed to the server process for validation using the business rules. If the data is valid, then the server process updates the database and communicates to the client; otherwise, it passes an error message to the client process.
However, with an increase in the amount of data to be transferred between the client and the server, the network load increases. The load at the server end also increases because the server process has to handle all validations. To overcome this, some of the business rules were shifted to the client process, and thus evolved the distributed client/server environment.
NOTE
If a client handles business rules, it is referred to as a fat client; if a server handles the business rules, it is referred to as a fat server.
Although the two-tier architecture addresses the problems of data duplication and data inconsistency (of the single-tier architecture), this model also has some inherent disadvantages:
Increased load on the server. All clients access a central server, thereby increasing the load on the server. This leads to deterioration in the server’s performance.
Increased network traffic. Obviously, with the increase in the amount of data transfer between the client and server, the network traffic also increases, thereby deteriorating the network’s performance.
Implementation of incremental upgrades is tedious. Huge client-side executables don’t facilitate incremental upgrade. Particularly, in the case of a fat client, incremental upgrade is tedious because the application’s source code has to be modified, recompiled, and redistributed.
Applications tightly coupled with the database on the server. The clients are configured to access a specific database located on the server. As a result, any change in the database at the server side results in reconfiguring the clients.
564 Project 5 CREATING A COM COMPONENT USING ATL
With the then available limited-powered processors, you could run only a single application at a time. As more powerful processors were developed, Microsoft envisioned the potential for running multiple applications concurrently, and subsequently designed the Windows operating system. Slowly, the need for communication between these applications running in parallel arose. To address this, Microsoft enhanced the Windows operating system by adding the Clipboard feature, which introduced the much-used cut/copy and paste operation. Can you imagine working without this feature?
The advent of the Clipboard eased the handling of compound documents — documents that can store varied types of data, such as text, graphics, video, and so on. However, Clipboard has its own disadvantages, the most obvious being the lack of intelligent data transfer. For instance, after you copy data from an application, such as Excel, and paste it into another application, such as Word, any change in the source data will not be reflected in the copy. In other words, after you copy the desired data from the Excel sheet to the Word document, if you make any changes to the Excel sheet, the changes will not be reflected in the Word document. As a solution to this problem, Microsoft introduced object linking and embedding (OLE). OLE 1.0 was the first version released to the market in 1992.
OLE was Microsoft’s move toward object-oriented programming (OOP), which was the new paradigm that the IT industry was shifting to. Using OLE, you could link or embed, for instance, an Excel worksheet into a Word document. Considering the previous example, if you link the Excel worksheet with the Word document, then any changes to the source worksheet will automatically be updated in the Word document. The other advantage with OLE is that you can open the embedded or linked worksheet directly from within Word and edit it. Despite this advantage, OLE 1.0 was not well received by the software vendors or developers because of the complexity in its implementation. However, it led to the evolution of the component technology, which is a spin-off of the object-oriented technology. This technology treats each compound document object as a component. A component is a self-contained object that can be used in any application, thereby resulting in reusability. You can correlate this concept to the very popular Plug and Play feature.
Microsoft released the next version, OLE 2.0, in 1993. OLE 2.0 was an enhanced version of OLE 1.0. Besides the compound documents, OLE 2.0 encompassed a number of other object-based services. One of the most noted services was inplace activation, also called visual editing. This service enabled users to edit all data
INTRODUCTION TO COM Chapter 17 565
in a compound document from within the container application, without needing to open any other application. As it was realized later, OLE 2.0 was designed based on the component technology, which is now famous as the Component Object Model (COM).
Now that you have an idea of the objective addressed by COM — reusability — let me explain the role of COM in the application models that I earlier discussed. Based on the definition of a three-tiered application, it is evident that the business services layer should be independent of the user services layer and the data services layer. Besides being independent, business services should also be reusable, which implies that the code used to create a service should be reusable. The immediate technology that comes to mind as a solution provider is OOP.
OOP resulted in a pool of objects that couldn’t interact with each other “freely.” Interaction posed a problem because of the varied platforms, networks, and application types across which the objects had to interact. Although OOP promised to enable reusability, it couldn’t deliver results to its full potential. That is where COM gained its ground.
What Is COM?
Many schools of thought address this question. The two predominant ones that precisely describe COM are the following:
COM is a specification. COM is a specification put forth by Microsoft
— it is a document describing the method of creating applications based on components.
COM is a standard. COM is a binary standard specified by Microsoft for creating software components. This defines the standards and other specifications (as stated earlier) required to create components that can interact with each other regardless of the language and tool used to develop them.
In other words, COM provides the following features and functionality:
The binary standard with which you can create reusable components that can interact with each other.
COM services or APIs, available as part of the COM library. The COM library is part of the operating system for Win32 platforms, whereas it is available as a separate package for other operating systems.
566Project 5 CREATING A COM COMPONENT USING ATL
COM eases the upgrading process of your application.
COM provides location transparency, thereby enabling distributed processing.
COM components are cross-language compatible, meaning that a component in C++ can talk with another in Visual Basic.
NOTE
The interoperability between COM components is possible because of the binary standard followed commonly by these components. In simpler words, COM defines a standard that any program should adhere to after it is translated to machine code. As a result, a component written using Visual Basic can easily interact with a component using Visual C++ because both, when translated to machine language, will speak the same language.
Although the components are language independent, there is one restriction that COM enforces — the code generated using any language should be able to handle function calls through pointers. You will be able to appreciate this rule once you know the reason for it, which you will learn in the following discussion.
All of these features of COM make it the preferred technology to implement the business layer of an application.
Issues Addressed by COM
To understand the facilities provided by COM, and the need for COM, you need to understand the rationale for its existence. Microsoft has proposed COM as a solution for the following list of problems:
People have been reusing binary code for a long time — it started with the libraries of C and C++. However, until Windows popularized the use of dynamic link libraries (DLLs), most of the code reuse was static in nature, because the reused code was appended to the main executable, which resulted in bigger executables. DLLs allowed reusable code to be packaged into different DLLs, and these DLLs could be loaded by the main executable when required. This resulted in several advantages, such as smaller executables, the ability to load DLLs dynamically, and the use of shared DLLs.
568 Project 5 CREATING A COM COMPONENT USING ATL
COM components are nothing but objects. The only difference between COM objects and other object-oriented objects is that the COM objects sign a “treaty” to follow common standards, thus ensuring that they all speak the same language. This is the feature that makes interoperability possible.
Now the question is, what makes all this possible? What unites the otherwise varied components (variations in terms of language)? All this is possible because of one prominent feature of any COM component — the interface.
Before looking at what are interfaces and how to implement them, you need to first learn about the types of COM components that you can create.
Types of COM Components
A COM component can be either a server or a client. A component that uses the services of another component (through its interfaces) is referred to as a COM client and the other component that offers the service is referred as the COM server. For instance, while working in Microsoft Word, if you choose Insert, Object, a list of COM components is displayed. These components are classified as COM servers because they expose functionality that can be consumed by other components. Suppose that you choose a component from the list, such as the Calendar control. In such an instance, the Calendar control is the COM server and the current Microsoft Word document, in which you are inserting the control, is the COM client.
Earlier in the chapter, I stated that COM provides transparency in terms of the location of components. To achieve this, based on the location of components, COM servers are classified as in-process servers and out-of-process servers. Out- of-process servers can further be either local servers or remote servers.
In-process servers are implemented as DLLs and are loaded into the client’s process space.
DLLs are libraries that are loaded dynamically into the process space allocated for the client (which invokes the DLL). Because these DLLs reside in the same address space as that of the client, the inherent advantage is the speed of servers. Implementing a DLL has the following advantages:
DLLs are faster than EXEs because they reside in the same address space as that of the client.
DLLs are efficient because the client has a direct access to the server component.
