.NET/COM interoperability in depth: comprehensive techniques and strategies
The .NET and COM Interoperability Handbook will help you move your Windows(-based software into the future without abandoning the investments you've already made. Writing from the perspective of the experienced COM/COM+ developer, Alan Gordon offers the most realistic, in-depth coverage of .NET/COM interoperability ever presented. He illuminates all your .NET/COM interoperability options, offering practical advice for both migration and long-term coexistence. Coverage includes:
About This Series.
1. What's in a Name?
Microsoft .NET. XML Web Services. XML. SOAP.The .NET Framework. The .NET Framework SDK. The CLR. MSIL. The .NET Framework Class Library. ADO.NET. ASP.NET. Windows Forms. Other Microsoft.NET Technologies. Stale .NET-Related Names. Comparing .NET to Java. Summary.
What is a Software Component? What Is a Software Bus? Life Cycle Management. Programming Language Independence. Location Transparency. Extensibility. Versioning. Summary.
Describing The CLR. Defining Managed Code and Managed Data. Types. Assemblies. MSIL. How the Runtime Locates Assemblies. Garbage Collection. Code Access Security. Summary.
Getting Started with C#. Creating a Simple Hello World Application. Compiling and Linking the Hello World Application. The Basic Operators in C#. The Primitive Types Available in C#. Creating and Manipulating a String in C#. Creating and Using an Array in C#. Declaring classes in C#. Using Inheritance in C#. Flow of Control Constructs. Exception Handling in C#. Summary.
Introduction. COM Interop. Using COM Objects from .NET Clients. Using .NET Objects from COM Clients. Platform Invoke. Summary.
Object Life Cycle Implications. Using COM Events in Managed Code. Using COM-Rich Error Information in Managed Code. Multithreading and COM Apartments. Primary Interop Assemblies. Advanced Interop Marshaling Considerations Using ActiveX Controls in Managed Code. Performance Considerations. Advanced Aspects of Pinvoke. Summary.
Object Creation. Object Life-Cycle Implications. Overloaded Methods. COM Apartments. Using Types from the .NET Framework Class Library. Class Interfaces Revisited. Rich Error Information. Using Windows Forms Controls. ComVisible. Handling Managed Events from a COM Client. Deploying COM/Win32 Applications That Use .NET Components. Summary.
A Couple of Big Caveats. Creating a Serviced Component. Creating a Client. The Implementation of the Serviced Component Methods. Summary.
Web Services: The Big Picture. The ASP.NET Web Services Infrastructure. Creating a Simple Web Service. Creating and Consuming a Web Service with Visual Studio .NET. XML Web Services and Data. Returning Data Using a Dataset. Transactions. Summary.
Why Do We Need .NET Remoting? .NET Remoting Architecture. Categories of Remotable Types. A Simple .NET Remoting Client and Server. Configuration. Code Organization. Soapsuds. Hosting Your Server in IIS. Interoperation Between COM+ and Web Services. Summary.
Migration versus Interop. Vertical Migration. Horizontal Migration. Summary.
At the company for which I work we have just begun the process of planning our migration to .NET. This process has been fraught with both political and technical challenges. The purpose of this book is to help you with the technical challenges of .NET migration, in particular using COM Interop. The only help I can give you on the political challenges is: Beware of people who want to use .NET just because it's "cool", because Microsoft says you should, or to pad their resume. Also, beware of people who don't want to use .NET because of personal biases against Microsoft. Decisions about what technologies should be used should be based on business objectives first, customer satisfaction second, and ease of development and maintenance third. Other agendas must be brushed aside.
But enough about politics, this is a technical book with a target audience of intermediate to advanced COM/COM+ programmers who are looking to make the move to .NET. I assume some knowledge of COM and COM+, but I don't assume any prior knowledge of .NET. This book also functions as a .NET tutorial for COM programmers. I start from first principles defining at an advanced level all of the technologies in the .NET Framework. I have included chapters on the CLR, a one-chapter tutorial on the C# programming language, and chapters on .NET Web Services, and .NET Remoting. All of these chapters are there to provide you with the foundational knowledge that you need to understand the meat of the book, which is the "pure" Interop material in chapters 6 through 9, 11 and chapter 12. Although these only chapters make up slightly more than 40% of the chapter count of the book, they make up about two-thirds of the page count. Chapters 7 and 8, which cover advanced topics in Interop, are particularly "beefy" chapters.
My pedagogical style in writing is similar to the teaching style that I have used at UCLA Extension. My approach when teaching new concepts is as follows:
Most of the chapters start by explaining the problem that the .NET technology that you are about to learn about will solve. Next we discuss how this .NET-related technology works and how you will use it. You will find lots of analogies, diagrams and screen shots. Finally, in most of the chapters we will 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 in that chapter. I show you how to build the example program in enough detail that a beginning C# programmer should have no problem following along. I think you will learn the most by following along and 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 Web site for this book.
A link is provided at that Web site for downloading the sample programs for this book.
The sample programs are provided in a self-extracting file. When expanded, a directory structure is created, rooted in c:\OI\NetCom. The sample programs are in directories Chap01, Chap02, and so on. All the samples for a given chapter are in individual folders within the chapter directories. The names of the folders are clearly identified in the text. This book is part of the Integrated .NET Series. The sample programs for other books in the series are located in their own directories underneath c:\OI, so all the .NET examples from all books in the series will be located in a common area as you install them.
All of the source code examples in this book are in C#. I agonized for a long time over which .NET language to use for my source code examples. Although the .NET framework supports a wide variety of programming languages, the two languages that will be the most widely used are C# and VB.NET. Most of the source code examples in the .NET Framework documentation are implemented in both C# and VB.NET. I have seen some .NET books take this approach. After experimenting, I realized that since both languages share a common library, and VB.NET has been endowed with first-class object-oriented capability, there are few semantic differences between C# and VB.NET. The differences between the two languages are primarily syntactic. The choice of which language to use really comes down to which syntax you are most comfortable with. I decided, therefore, that it would be a waste of time—yours and mine—for me to try to write the examples in both languages. Therefore, I chose to do everything in C#. Don't despair if you are a VB programmer; my own experience has been that it is very simple to translate .NET code from VB.NET to C#, and vice versa.
The 12 chapters in this text are divided into three sections. The first five chapters contain introductory and foundational material. Chapter 1 is an executive overview of the names in the .NET namespace. Chapter 2 compares .NET to COM. Chapter 3 explains in detail how the CLR works. Chapter 4 is an introduction to C# and in Chapter 5 you will get the opportunity to build your first .NET component. If you are already familiar with .NET, you can skip some or all of these first chapters.
The meat of the book is in chapters 6, 7, and 8. In these chapters you'll learn all about COM Interop and PInvoke, too. In Chapter 6 I cover the basics of calling unmanaged code from a .NET component or application and calling a .NET component from an unmanaged client. I also cover the basics of Pinvoke. In Chapter 7 I dive into advanced aspects of .NET to COM Interop, that is, calling COM components from managed code. In this chapter I also cover some advanced aspects of Pinvoke. In Chapter 8 I cover advanced aspects of COM/Win32 to .NET Interop, that is, calling .NET components from unmanaged code.
The last four chapters are what I like to call "special topics". In Chapter 9, I discuss serviced components, which is the name in the .NET Framework for managed components that use the COM+ Services. In Chapter 10, I discuss XML Web Services. In this chapter I will give you some tips on exposing your existing COM+ applications as XML Web Services. In Chapter 11, I discuss .NET Remoting, which you can think of as the replacement for DCOM in the .NET Framework. In this chapter I also introduce you to the COM+ Web Services feature which is available in Windows XP and Windows .NET Server. This functionality allows you to expose any COM+ application as an XML Web Service simply by checking a box. Finally, in Chapter 12, I provide some best practices for incremental migrations.