Preface

Team-Fly    

  
.NET and COM Interoperability Handbook, The
By Alan Gordon
Table of Contents

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 :

  1. Explain why it is important for the reader to learn the new concept? What problem does the new concept solve? Why should the reader care about it?

  2. Explain the concept, in theory first, using copious analogies and diagrams.

  3. Have the student use the concepts and theories they have just learned in a hands-on demonstration. The demonstration does not have to be complicated; it just has to illustrate the concept that the student is trying to learn.

  4. Summarize what the reader should have just learned.

  5. Along the way, point the reader to additional resources to consult for further information.

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. The URL for the Web site is: www.objectinnovations.com/dotnet.htm. 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 timeyours and minefor 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.

A few words about my code listings: You will notice that there are two types of code listings. Some have numbered lines and some do not. Code that does not have a listing number is usually a short snippet that is included to illustrate a point. Usually I will not explain this code in any significant depth. An example is shown below.

 private void cmdAnnounce_Click(object sender, System.EventArgs e)     {     PlayByPlayHandler microphone1=new   PlayByPlayHandler(ReceivePlayByPlay);     PlayByPlayHandler microphone2=new       PlayByPlayHandler(ReceivePlayByPlay2);     LakerAnnouncer chickHearn=new LakerAnnouncer();     chickHearn.Microphone+=microphone1;     chickHearn.Microphone+=microphone2;     chickHearn.Announce(5); } 

Code which is important enough that I want to explain it line by line will have individually numbered lines, as shown here.

 1.  public delegate void PlayByPlayHandler(2.    string strLines); 3.  public class LakerAnnouncer 4.  { 5.    public LakerAnnouncer() 6.    { 7.          mLines=new StringCollection(); 8.          mLines.Add("It's in the refrigerator"); 9.          mLines.Add("The butter's getting hard"); 10.         mLines.Add("the jello is jigglin"); 11.         mLines.Add("Slaaaam dunk!"); 12.         mLines.Add("Since Hector was a pup"); 13.         mLines.Add("Yo yoing up and down"); 14.         mRandNumGen=new Random(); 15.       } 16.       public void Announce(int numLines) 17.       { 18.         int r; 19.         string strLine; 20.         if (Microphone != null) 21.         { 22.           for (int i=0;i<numLines;i++) 23.           { 24.             r=mRandNumGen.Next(); 25.             strLine=mLines[r % mNumLines]; 26.             Microphone(strLine + "\r\n"); 27.           } 28.         } 29.       } 30.       private StringCollection mLines; 31.       public PlayByPlayHandler Microphone; 32.       private Random mRandNumGen; 33. } 

If you already understand the .NET Framework you can skip chapters 1, 3, 4 and 5. If you are not interested in writing code, you can skip the hands-on part in the chapters. If you just want Interop and nothing but Interop, read chapters 69, 11 and 12. Either way, have fun, and I hope that you find this book informative. Let's get started.


Team-Fly    
Top
 


. Net and COM Interoperability Handbook
The .NET and COM Interoperability Handbook (Integrated .Net)
ISBN: 013046130X
EAN: 2147483647
Year: 2002
Pages: 119
Authors: Alan Gordon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net