没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
COM and .NET Interoperability
by Andrew Troelsen
ISBN: 1590590112
Apress © 2002 (816 pages)
For both new and seasoned developers, this reference provides you with all
you need to know about COM and .NET, and how to make them work
together for you.
Table of Contents
COM and .NET Interoperability
Introduction
Chapter 1 -
Understanding Platform Invocation Services
Chapter 2 -
The Anatomy of a COM Server
Chapter 3 -
A Primer on COM Programming Frameworks
Chapter 4 -
COM Type Information
Chapter 5 -
The Anatomy of a .NET Server
Chapter 6 -
.NET Types
Chapter 7 -
.NET-to-COM Interoperability— The Basics
Chapter 8 -
.NET-to-COM Interoperability— Intermediate Topics
Chapter 9 -
.NET-to-COM Interoperability— Advanced Topics
Chapter 10
-
COM-to-.NET Interoperability— The Basics
Chapter 11
-
COM-to-.NET Interoperability— Intermediate Topics
Chapter 12
-
COM-to-.NET Interoperability— Advanced Topics
Chapter 13
-
Building Serviced Components (COM+ Interop)
Index
List of Figures
List of Tables
COM and .NET Interoperability
ANDREW TROELSEN
Copyright © 2002 by Andrew Troelsen
All rights reserved. No part of this work may be reproduced or transmitted in any form or
by any means, electronic or mechanical, including photocopying, recording, or by any
information storage or retrieval system, without the prior written permission of the
copyright owner and the publisher.
ISBN (pbk): 1-59059-011-2
Printed and bound in the United States of America 12345678910
Trademarked names may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, we use the names only in an editorial fashion
and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
Technical Reviewers: Habib Heydarian, Eric Gunnerson
Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore,
Karen Watterson, John Zukowski
Managing Editor: Grace Wong
Copy Editors: Anne Friedman, Ami Knox
Proofreaders: Nicole LeClerc, Sofia Marchant
Compositor: Diana Van Winkle, Van Winkle Design
Artist: Kurt Krames
Indexer: Valerie Robbins
Cover Designer: Tom Debolski
Marketing Manager: Stephanie Rodriguez
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175
Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag
GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany.
In the United States, phone 1-800-SPRINGER, email <orders@springer-ny.com>,
or visit http://www.springer-ny.com.
Outside the United States, fax +49 6221 345229, email <orders@springer.de>, or
visit http://www.springer.de.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite
219, Berkeley, CA 94710.
Phone: 510-549-5930, Fax: 510-549-5939, Email: <info@apress.com>, Web site:
http://www.apress.com.
The information in this book is distributed on an "as is" basis, without warranty. Although
every precaution has been taken in the preparation of this work, neither the author nor
Apress shall have any liability to any person or entity with respect to any loss or damage
caused or alleged to be caused directly or indirectly by the information contained in this
work.
The source code for this book is available to readers at http://www.apress.com in
the Downloads section. You will need to answer questions pertaining to this book in
order to successfully down-load the code.
This book is dedicated to Mary and Wally Troelsen (aka Mom and Dad). Thanks for
buying me my first computer (the classic Atari 400) so long ago and for staying awake
during my last visit when I explained (in dreadful detail) how System.Object is so much
better than IUnknown. I love you both.
Acknowledgments
As always, I must give a very real and heartfelt thanks to all of the fine people at Apress.
First, thanks to Gary Cornell and Dan Appleman for building such a great place for
writers to do their work. A mammoth thanks to Grace Wong for gently pushing me
forward in order to get this book out on time and for putting up with me in general. And
thanks to Stephanie Rodriguez and Hollie Fischer for their awesome work in spreading
the word about Apress titles both at home and across the globe.
A huge thanks also goes to Ami Knox, Nicole LeClerc, Sofia Marchant, and Anne
Friedman, all of whom did fantastic jobs smoothing over any grammatical glitches on my
part. Thanks to Habib Heydarian and Eric Gunnerson for providing excellent technical
assistance. Further thanks to Diana Van Winkle, Kurt Krames, and Tom Debolski for
making the book look respectable and professional inside and out. Special thanks to
Valerie Robbins for working on (yet another) tight dead-line in order to index these
chapters.
As for those individuals a bit closer to home, a heartfelt thanks to all my coworkers at
Intertech, Inc. (http://www.intertech-inc.com), for making my "real job" a
wonderful place to be. The previous praise does not apply to Tom Salonek, whom I still
don't care much for at all (... well, maybe just a little). Further thanks are in order for my
family and friends for remaining patient when I became "just a bit grumpy" during the last
month of this project. Last but not least, I must thank my wife Amanda for supporting me
through yet another stint of sleepless nights and for remaining positive and encouraging
when I was anything but. Thanks all!
Introduction
The funny thing about writing a book on COM and .NET interoperability is that one
author could craft a five- to ten-page article describing the basic details that you
must understand to get up and running with interop-related endeavors. At the same
time, another author could write volumes of material on the exact same subject. So,
you may be asking, how could this massive discrepancy between authors possibly
exist?
Well, stop and think for a moment about the number of COM-aware programming
languages and COM application frameworks that exist. Raw C++/IDL, ATL, MFC,
VB 6.0, and Object Pascal (Delphi) each have their own syntactic tokens that hide
the underbelly of COM from view in various ways. Thus, the first dilemma you face
as an interop author is choosing which language to use to build the COM sample
applications.
Next, ponder the number of .NET-aware programming languages that are either
currently supported or under development. C#, VB .NET, COBOL .NET, APL .NET,
PASCAL .NET, and so on, each have their own unique ways of exposing features of
the CTS to the software engineer. Therefore, the next dilemma is choosing which
language to use to build the .NET applications.
Even when you solve the first two dilemmas and choose the languages to use
during the course of the book, the final dilemma has to do with the assumptions
made regarding the readers themselves. Do they have a solid understanding of IDL
and the COM type system? Do they have a solid understanding of the .NET
platform, managed languages, and metadata? If not, how much time should be
spend pounding out such details?
Given the insane combinations of language preferences and reader backgrounds, I
have chosen to take a solid stance in the middle ground. If I have done my job
correctly, you will walk away from this text with the skills you need to tackle any
interop-centric challenge you may encounter. Also, I am almost certain you will
learn various tantalizing tidbits regarding the COM and .NET type systems.
My ultimate goal in writing this book is to provide you with a solid foundation of
COM and .NET interoperability. To achieve this goal, I have chosen to provide
material that defines the finer details of the COM and .NET architectures. For
example, over the course of the first six chapters, you will learn how to
programmatically generate and parse COM IDL, dynamically generate C# and VB
.NET source code on the fly (via System.CodeDOM), and build .NET applications
that can read COM type information. After all, when you need to build a software
solution that makes use of two entirely unique programming paradigms, you had
better have a solid understanding of each entity.
However, once this basic foundation has been laid, the bulk of this book describes
the process of making COM and .NET binaries coexist in harmony. As an added
bonus, I cover the process of building .NET code libraries that can leverage the
services provided by the COM+ runtime layer (via System.EnterpriseServices).
Now that you have the big picture in your mind, here is a chapter-by-chapter
breakdown of the material:
Chapter 1: Understanding Platform Invocation Services
I open this examination of COM/.NET interoperability by focusing on the role of a
single .NET class type: DllImportAttribute. In this chapter, you learn how to access
custom C-based (non-COM) DLLs as well as the Win32 API from a managed
environment. Along the way, you investigate how to marshal C structures, interact
with traditional callback functions, and extract exported C++ class types from within
a managed environment. This chapter also examines the role of the Marshal class,
which is used in various places throughout the book.
Chapter 2: The Anatomy of a COM Server
The point of this chapter is to document the internal composition of a classic COM
server using raw C++ and COM IDL. Given that many COM frameworks (such as
VB 6.0) hide the exact underpinnings of COM, this chapter also examines the use of
the system registry, required DLL exports, the role of the class factory, late binding
using IDispatch, and so on. As you might guess, the COM server you construct
during this chapter is accessed by managed code later in the text.
Chapter 3: A Primer on COM Programming Frameworks
Given that you build a number of COM servers over the course of the book, this
(brief) chapter provides an overview of two very popular COM frameworks: the
Active Template Library (ATL) and Visual Basic 6.0. Knowledge mappings are
made between the raw C++ server created in Chapter 2 and the binaries produced
by the ATL/VB 6.0 COM frameworks. Along the way, you also explore the key COM
development tool, oleview.exe.
Chapter 4: COM Type Information
This chapter examines the gory details of the COM type system, including a number
of very useful (but not well-known) tasks such as constructing custom IDL attributes,
applying various IDL keywords such as [appobject], [noncreatable], and so forth.
More important, this chapter also illustrates how to read and write COM type
information programmatically using ICreateTypeLibrary, ICreateTypeInfo, and
related COM interfaces. This chapter wraps up by examining how to build a
managed C# application that can read COM type information using interop
primitives.
Chapter 5: The Anatomy of a .NET Server
The goals of this chapter are to examine the core aspect of a .NET code library,
including various deployment-related issues (for example, XML configuration files,
publisher policy, and the like). This chapter also provides a solid overview of a
seemingly unrelated topic: dynamically generating and compiling code using
System.CodeDOM. Using this namespace, developers are able to dynamically
generate code in memory and save it to a file (*.cs or *.vb) on the fly. Once you
have investigated the role of System.CodeDOM, you will have a deeper
understanding of how various interop-centric tools (such as aximp.exe) are able to
emit source code via command line flags.
Chapter 6: .NET Types
If you haven't heard by now, understand that the .NET type system is 100 percent
different than that of classic COM. Here, you solidify your understanding of the .NET
type system, including the use of custom .NET attributes. This chapter also
examines the role of the System.Reflection namespace, which enables you to
dynamically load an assembly and read the contained metadata at runtime. This
chapter also illustrates late binding under .NET and the construction of custom
managed attributes. I wrap up by showing you how to build a Windows Forms
application that mimics the functionality provided by ILDasm.exe.
Chapter 7: .NET-to-COM Interoperability—The Basics
In this chapter, the focus is on learning how to build .NET applications that consume
classic COM servers using a Runtime Callable Wrapper (RCW). You begin with the
obvious (and most straightforward) approach of using the integrated wizards of
Visual Studio .NET. Next, you learn about the tlbimp.exe tool (and the numerous
command line options). Along the way, you are exposed to the core conversion
topics, including COM/.NET data type conversions, property and method mappings,
and other critical topics.
Chapter 8: .NET-to-COM Interoperability—Intermediate Topics
This chapter builds on the previous one by examining a number of intermediate
topics. For example, you learn how .NET clients can make use of COM VARIANTs
and SafeArrays, COM Error Objects, COM enums, COM connection points, and
COM collections. Topics such as exposing COM interface hierarchies are also
examined in detail.
Chapter 9: .NET-to-COM Interoperability—Advanced Topics
Here you learn to import ActiveX controls and augment the work performed by the
aximp.exe command line utility to account for COM [helpstring] attributes that are
lost during the conversion process. Furthermore, this chapter examines the process
of manually editing the metadata contained in a given interop assembly. For
example, you learn how to support [custom] IDL attributes in terms of .NET
metadata and understand how to compile *.il files using ilasm.exe. This chapter also
describes how a COM type can implement .NET interfaces to achieve "type
compatibility" with other like-minded .NET types. You wrap up by learning how to
build a custom type library importer application using C#.
Chapter 10: COM-to-.NET Interoperability—The Basics
This chapter focuses on how COM clients (written in VB 6.0, C++, and VBScript)
can make use of .NET types using a COM Callable Wrapper (CCW). Here, I cover
class interfaces, the tlbexp.exe/regasm.exe command line tools, and various
registration and deployment issues. This chapter also examines how a COM client
can interact with the types contained in the core .NET assembly, mscorlib.dll.
Chapter 11: COM-to-.NET Interoperability—Intermediate Topics
This chapter builds on the materials presented in Chapter 10 by examining how
.NET enumerations, interface hierarchies, delegates, and collections are expressed
in terms of classic COM. You also learn how to expose custom .NET exceptions as
COM error objects, as well as about the process of exposing .NET interface
hierarchies to classic COM.
Chapter 12: COM-to-.NET Interoperability—Advanced Topics
This advanced COM-to-.NET-centric chapter examines how a .NET programmer is
able to build "binary-compatible" .NET types that integrate with classic COM. You
see how a .NET type can implement COM interfaces, and you also get a chance to
explore the details of manually defining COM types using managed code. This
chapter also examines how to interact with the registration process of an interop
assembly. The final topics of this chapter address the process of building a custom
host for the .NET runtime (using classic COM) and the construction of a custom
.NET-to-COM conversion utility.
Chapter 13: Building Serviced Components (COM+ Interop)
Despite the confusion, .NET programmers are able to build code libraries that can
be installed under COM+. In this final chapter, I begin by examining the role of the
COM+ runtime and reviewing how it fits into n-tier applications. The bulk of this
chapter is spent understanding the System.EnterpriseServices namespace and
numerous types of interest. You learn how to program for JITA, object pools,
construction strings, and transactional support using managed code. I wrap up by
constructing an n-tier application using managed code, serviced components,
Windows Forms, and ASP .NET.
Now that you have a better understanding about the scope of this book and the
mindset I have regarding the material that follows, understand that I have written
this book based on the following assumptions about you:
§ You are not satisfied with clicking a button of a given wizard and thinking "I
guess it worked ... somehow ... I think." Rather, I assume you would love to
know the inner details of what that wizard does on your behalf and then
click the button.
§ You are aware of the role of COM, have created a number of COM servers,
and feel confident building COM solutions in the language mapping of your
choice. As well, I am assuming that you still find the process of learning the
finer details of COM a worthwhile endeavor. As you will see, most of the
COM servers built during the course of this book make use of VB 6.0,
unless a particular COM atom cannot be expressed using the vernacular of
BASIC. In these cases, I make use of the ATL framework.
§ You are aware of the role of .NET, have (at the very least) explored the
syntax of your favorite managed language, and (at the very most) created a
number of .NET applications during the process. While many of my
managed examples make use of C#, I also make use of VB .NET when
necessary.
剩余634页未读,继续阅读
资源评论
- 哈哈地图2014-04-23是讲COM如何与Net互相沟通的,可能很有深度,难懂
- teapot12342014-04-05This book is not a PDF of the print edition. Instead, it's a CHM converted to PDF format. Thus readability is not as good. The content of the book is OK.
kittyjie
- 粉丝: 237
- 资源: 1615
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功