Teaching my class at UCLA, an Introduction to COM/ActiveX Programming Using Visual C++, has given me a good background from which to write a book on COM and COM+. The target audience for this book is the same people who took my class at UCLA: intermediate-to-advanced programmers who have little or no knowledge of COM or COM+. By the time you finish this book, you should have enough knowledge of COM to begin using COM and COM+ professionally. My Approach
My objective is to teach you the 90 percent of COM that, as an application developer, you will use every day. Most of this is surprisingly simple. But there is another 10 percent of COM that is very difficult to understand and this is the part with which I have often seen otherwise excellent programmers become bogged down. It's not that these "10 percent" topics aren't important, it's just that you can get started and begin using COM without understanding all of these topics. Some "10 percent" topics that immediately come to mind are Apartments, custom marshaling, aggregation in general, manual marshaling of interface pointers, and aggregating the Free-Threaded Marshaler. I do cover all of these topics in this book, but be forewarned you probably won't understand these topics the first time you try to learn them; I certainly didn't. But don't get discouraged and don't let topics like this "bog you down" as you try to learn COM. This is not what COM and COM+ is all about. COM is about making it simple for you to build location transparent and programming-language independent software components. COM+ is all about facilitating the development of enterprise-class, distributed applications. The objective of neither of these technologies is to aggregate the Free-Threaded Marshaler. Don't allow yourself to become lost in the minutia of COM as many people do. When I cover topics that I know tend to bog down people (like Apartments), I caution you to just keep going if you don't understand it right away. Please follow this advice. You will understand the other concepts as you begin using COM professionally and start tackling more difficult problems.
Visual C++ and Visual Bask
The primary programming language used in this book is Visual C++. The world seems to be turning away from languages like Visual C++ and to languages like Visual Basic, Java, and Delphi. Unfortunately these languages hide so much of the under-lying details of COM that it is difficult to understand what's going on under the hood if you only use these languages. Only in Visual C++ can you implement the IUnknown interface, and create your own class factory, as you will do in Chapter 4. I strongly believe that the additional insight that you gain by doing this is well worth the additional complexity. In addition, there are certain things that you can do in COM+ that you simply cannot do from Visual Basic 6, such as building an object that will run in the thread-neutral apartment and using object pooling. Once you understand COM+ using Visual C++, transferring your knowledge to a more productive development environment, like Visual Basic, will be easy. Don't worry; you don't have to be a C++ Guru to read this book. I really think that a beginning C++ programmer will have little problem following along with the code in this book. I do use Visual Basic to build some of the client programs. I mainly did this to save time. It is much easier to build GUIs with Visual Basic than Visual C++. In addition, using Visual C++ for server-side development and Visual Basic for client-side development is a popular combination that I have seen on a number of projects recently.
Use of the Word Component
If you read any books about COM you quickly see that the word component has two popular uses:
It refers to a binary file (a Dynamic Link Library or Executable) that implements one or more COM classes it refers to a single COM class
Personally, I prefer the first definition of the word mainly because I believe that a COM class adequately describes the individual pieces of functionality that may reside in a single binary. People who use component to refer to a COM class usually use component server or just server to refer to the binary in which several components may be hosted. When you start using COM+ you will see that Microsoft refers to COM+ classes as components (even though they are not completely consistent either). I'm hardly a zealot on these kinds of issues so I concede the argument for now. I think there still are a few places in this book where my old bias shows through. I believe that in all places it is obvious from the context whether I am referring to a single COM class or a binary file in which several classes reside. The Book's Structure
Of the 15 chapters in this text, eight chapters (1-8) are essentially pure COM, two chapters (9 and 10) are primarily COM with some COM+, and five chapters (chapters 11-15) are pure COM+. The simple reality is that you cannot understand COM+ without first understanding COM. COM+ is really just a vastly improved and more tightly integrated version of Microsoft Transaction Server along with some key refinements to the underlying COM infrastructure such as Thread Neutral Apartments. A book that only covered COM+ features would only be usable by people who understand COM already. I covered both COM and COM+ because I wanted this book to be usable by people who are new to COM. Having said that, I teach you only enough COM so that you can understand COM+. COM+ was designed primarily to facilitate the development of enterprise-class, distributed applications, so there are many COM topics (ActiveX controls for instance) that are not covered in this book because they have little relevance to enterprise-class development and COM+.
Most of the chapters start by explaining a problem that can be resolved by using COM/COM+. That is followed by a discussion of how the COM-related technology works and how you will use it. This discussion includes a lot of analogies, diagrams, and screen shots. Finally, in most of the chapters you build a demonstration program. I don't think any of the demonstration programs are terribly sophisticated (they weren't meant to be). The example programs are designed to reinforce the topic that you are learning in that chapter. I show you how to build the example program in enough detail that a beginning Visual C++ programmer (if you can work through the Scribble tutorial in the Visual C++ documentation you're good to go) should have no problems following along. I think you will learn the most by building the example programs yourself, but if you don't want to go through the steps yourself you can download the demonstration programs from the FTP site for this book ftp://ftp.prenhall/pub/ptr/windows_technology.056/gordon.
Part 1: Laying the Foundation
In the first chapter, I define all the names in the COM namespace. Confused as to what COM, OLE, ActiveX, and the Windows DNA are? Chapter 1 defines these names and more. In Chapter 2 you learn a number of object-oriented concepts that are important pre-requisites to understanding COM. In particular, you learn what it means to be object-oriented and I define a class, an object, and an interface. The discussion of interfaces in this chapter is particularly important. In Chapter 3 I define the notion of a software bus and use it to explain some of the intricacies of COM: GUIDs, the IUnknown interface, Type libraries, Marshaling, and how COM uses the registry. In Chapter 4 you build your first COM class in an in-process server. You do this "from scratch" without the help of any wizards. You also build a COM client application. In Chapter 5 you learn how to build a COM server much more easily using the Active Template Library (ATL) and the ATL Object Wizard. All of the work that you do from Chapter 5 on uses ATL and/or Visual Basic.
Part 2: Digging Deeper In Chapter 6 you learn how to build an out-of-process server. In Chapter 7 you learn about Automation. In Chapter 8 you learn about connection points. These 8 chapters are the pure COM material in this book. In Chapter 9 I discuss COM threading and Apartments. This chapter features some material (primarily Thread Neutral Apartments) that will be new to COM programmers who have not used COM+. In Chapter 10 I discuss DCOM and again in this chapter I discuss, briefly, some new topics like the Kerberos Security Support Provider.
Part 3: COM+
Chapter 11 begins our discussion of COM+. In Chapter 11 I discuss the COM+ architecture. You learn about Context's and Interception. You also learn how to configure a component using the Component Services Explorer. In Chapter 12 you learn about each of the Services that COM+ provides to components: fine-grained security, transactions, concurrency, store-and-forward method invocation (queued components), loosely-coupled events, load balancing, improved scalability thru just In Time activation and pooling of objects, and database connections. Chapter 13 is a big chapter in both size and significance. In this chapter you put it all together and build a 3-tiered, thin-client application using all the technologies in the Windows Distributed interNet Applications (DNA) Architecture: COM+, Active Server Pages, ADO, SQL Server 7, Visual C++, and Visual Basic. In Chapter 14 you use Queued Components to enhance your application. In this chapter you also learn how to use the Microsoft XML parser. Finally, in Chapter 15 you learn how to use COM+ Events. The Intended Audience
You need to have at least a beginner's level knowledge of both Visual C++ and Windows Programming, in general, in order to understand this book. Also, the more you know about object oriented concepts, pointers, distributed processing, and multi-threading the easier time you will have learning COM. None of this knowledge is required to understand this book, as I do start from first principles. But trust me, the more you understand these concepts, the easier time you will have understanding COM, whether you learn from this book, another book, or a class. Conventions used in this book
This book uses different features to help highlight key information. These features are discussed in the following sections.
You will notice that there are three types of code listings. Some do not have a listing number, some have a listing number, and some have a listing number and numbered lines. Code that does not have a listing number is usually a short snippet that is included to illustrate a point.
Sidebars are used to present parenthetical information: information that is related to the topic at hand but does not flow directly with the main body of the chapter. In a few cases, I also use sidebars as a soapbox where I present information that is strictly my opinion, such as when I discuss my idea for a new COM naming scheme in Chapter 1. If you put a "by the way" or a "for your information" before each sidebar then you've got the idea.
Notes are used as small sidebars. Whenever I want to present parenthetical information that consists of only a sentence or two, I make it a note.
Tips are used to provide recommendations or to present information that can either save you time or prevent you from encountering minor problems.
Warnings are used to present information that is so important that I want it to stand out. I also use warnings to inform you of things that you must do to avoid serious problems. It is particularly important for you to pay heed to warnings that are associated with the example programs. In most cases, not following the warning will cause the example program not to work. About the FTP Site
The FTP site for this book can be found at the following URL: ftp://ftp.prenhall/pub/ptr/windows_technology.056/gordon. The FTP site contains the errata for the book. It also contains the source code for all the example programs in the book. If you do not wish to build the example programs yourself, or you encounter problems building the examples, you can download the finished programs from the FTP site. Acknowledgements
My name may be listed as the sole author of this book, but it took the work of a team of people to make this happen. Let me begin by thanking the people at Prentice Hall who were directly involved in the production of the book: my acquisitions editor, Tim Moore, my developmental editor, Jim Markham, and my technical reviewer, John Vacca. Special thanks to Jim for being so patient with me. Thanks also to Scott Suckling and the team at MetroVoice who did the final production work to turn my manuscript into the book you now hold in your hand. Next, I would like to thank all the people at UCLA Extension. It was through my work there that I was given the opportunity to write this book. Standout people I would like to mention are Terry Warner, Ben Stein, Carolyn Diliberto, Joseph Mueller, Donald Haushnect, and Robert Abnous. At Logicon's Advanced Technology Group I would like to thank everyone on the CAMPS project including Mark Flores, Rick Clough, Betty Hampikian, Curtis Enge, Marilyn Leatherwood, Theresa Millette, and Ward Keenan. I would like to especially thank everyone who worked for me when I was the technical lead on the Channels component including Bill Bulaich, Curt Miller, Sanjukta Chowdhurry, and Steve Caton. Others I would like to thank at Logicon include Roger Fuji, Vince Goshi, Steve Altstatt, Mike Twymann, and Steve Cooper. Many thanks to my lunch buddies at Logicon, Bill Bulaich and Paul Firnett. Since late August 1999 I have been working at a new company called FieldCentrix, fieldcentrix. This startup company is located in Irvine, California, and develops software for field service workers using the latest technology in wireless communications, rugged handheld PCs, and the Windows CE operating system. Those I would like to thank at FieldCentrix include Dave Key (the company founder), Dr. Albert Lin (who hired me), Paul Osterhoudt, Gaurav Jalota, Ashu Shende, Calvin Shen, Lap Tran, and Kevin Bates.
Many thanks to friends who have influenced me including: Dr. Quintus Jett, Dr. George Shaw, Dr. Shozo Koshigoe, Carlos Castellanos, and Ruel Nuqui. Next, I would like to thank my family. Kudos to my older brother Winston Gordon, Jr. When I was an Aerospace Engineering major at UCLA, he scoffed when I was learning FORTRAN-told me that C was the way to go. Eventually, I bought books on C and on C++. He started me on a journey that culminated in my writing this book. I would also like to thank my parents, Agatha and Winston Gordon, Sr. I can never repay you for all you've done for me so I say thank you. Other members of my family I would like to thank include Vera Littlejohn, Dr. Mary Marin, Alma Horper, Kaylah Marin, Mikalynn Todd, Amber Saucedo, and Sharine Dinwiddie. Finally, and most importantly, I would like to thank the two special women in my life: my wife Tamber Lynn and my daughter Bria Almarie. Thank you both for all the love and support you gave me and continue to give me, before, during, and after this book was written. You sacrificed so much to make this a reality. Thank you, Tamber, for encouraging me when I was ready to give up this project and I'm sorry for all the times I wasn't at my best while writing this book. Bria, forgive me for the times I couldn't play with you while I was writing this book. I hope one day you will understand that I did it to help build a better life for us. To anyone I forgot, my apologies and heartfelt thanks.