About the Book
The one-stop guide to how COM and COM+ fit
into the .NET world
Microsoft® .NET is here, but COM and
COM+ will be a valuable part of every Microsoft Windows® programmer’s
toolbox for years. This in-depth programming guide explains how and where
COM and COM+ fit into the new .NET world, demonstrates the new technologies
that are available in .NET, and shows how to interoperate between COM/COM+
and .NET. It explains how to use Microsoft Visual Studio® .NET to write
traditional COM objects, how to use COM objects in .NET code, and how to
use .NET objects as COM objects. It also discusses COM+ topics such as
disconnected applications and subscriptions. The authors show you both
theoretical and practical approaches—explaining how the mechanisms work
and also providing practical advice with sample code to show how to handle
interoperability.
Topics covered include:
The .NET view of COM and COM+
• COM, COM+, and .NET
• Using COM components in .NET code
• Using .NET components in COM applications
• Overview of COM+ coding for .NET
Writing COM code
• Attributed programming
• Active Template Library (ATL) and ATL
Server
Writing COM+ code
• A simple COM+ example
• Working with disconnected applications
• Creating subscriptions
• Web application scenarios
Interoperability
• Interacting with unmanaged code
• Advanced interaction
• Working with predefined interfaces
Includes sample code on the Web
Table of Contents
Acknowledgments
xv
Introduction xvii
PART I THE .NET VIEW OF
COM AND COM+
1 COM and .NET
3
Is COM
Dead? 3
How Does
COM Work in the .NET World? 5
Differences
in the COM and .NET Philosophies 7
Locating Components 7
Component Identification 8
Object Lifetimes 9
Determining Object Capabilities 9
Constructors and Destructors 10
Nondeterministic Finalization 10
Error Handling 13
Type Information 13
Visibility 14
Data Handling 15
Event Handling 19
Using
the IDE to Access Components 19
Locating the Component You Need 20
Simple COM Access Examples 21
2 COM+ and .NET
27
What
Is the Place of COM+ in the .NET World? 28
COM+
Problems and Improvements 29
Using
the Component Services MMC Snap-In 30
An Overview of the Interface 30
Creating COM+ Applications and Installing Components 34
Using
the .NET Framework Configuration MMC Console 47
Managing Components and Controls 48
Remoting Services Configuration 49
Defining a Runtime Security Policy 50
3 Using COM Components
in .NET Code 53
COM Interop:
Principles and Mechanisms 53
Interop Assemblies 54
Primary Interop Assemblies 54
Generating
Runtime Callable Wrappers 55
Using Visual Studio .NET 56
Using the TlbImp.exe Tool 57
Using the TypeLibConverter Class 61
Assemblies and the GAC 67
Generating and Installing Primary Interop Assemblies 73
How COM
Entities Are Converted 74
Dealing with Attributes 74
Importing Libraries 74
Importing Data Types 75
Importing Classes 83
Importing Interfaces 85
Importing Structures, Unions, and Enumerations 88
Importing Typedefs 90
Importing Modules 91
How to
Design COM Components for Use with .NET 91
Responding
to COM Events 94
Connection Points 95
Handling Events from a COM Source 96
Using
ActiveX Controls with .NET 99
Adding an ActiveX Control to the Toolbox 100
Using the Command Line 102
4 Using .NET Components
in COM Applications 103
The COM
Callable Wrapper 103
Object Identity 104
Object Lifetime 104
Standard COM Interfaces on .NET Components 105
Custom Interfaces on .NET Components 106
Generating
and Using COM Callable Wrappers 108
Using COM-Related Attributes 108
Creating a Type Library 114
Signing the Assembly 119
Registering the Component 119
Using .NET Components from COM Client Code 120
Exporting
Metadata to Type Libraries 125
Exporting Assemblies 125
Exporting Namespaces 126
Exporting Classes 127
Exporting Interfaces 128
Exporting Data Types 136
Exporting Value Types 137
Exporting Enumerations 138
How to
Design .NET Components for Use with COM 139
Provide a Default Constructor 139
Avoid Using Static and Overloaded Methods 139
Be Aware of Possible Naming Problems 139
Avoid Altering Interfaces 140
Define Event Source Interfaces 141
Use of Attributes 141
Provide HRESULTs 142
Use Versioning Correctly 142
Hosting
Windows Forms Controls in ActiveX Control Containers 143
Registry Entries 144
Example: Hosting a Windows Forms Control in Internet Explorer 145
Exposing
.NET Events in COM 148
Using Explicit Source Interfaces 149
5 An Overview of
COM+ Coding for .NET 155
COM+
Begins with COM 156
A Look at COM+ Interoperability 156
COM+ Design Goals 163
Transactions and COM+ 164
Messages and COM+ 166
COM+ Services 169
Understanding
the Role of DCOM in COM+ 172
How Does the Connection Work? 173
Connection-Oriented Data Flow Optimization 178
COM+-Specific
Issues 179
Application Types 179
Error Handling 182
Security 183
PART II WRITING COM
CODE
6 Attributed
Programming 189
What
Are Attributes? 190
How Do Attributes Work? 192
Using
Attributes in C++ Code 193
Walkthrough:
Creating a Simple COM Component 194
Seeing the Inserted Code 195
Adding COM Objects 196
Adding Methods and Properties 200
Testing the Component 203
Creating the Server by Hand 204
Basic
Attributed Programming 207
Creating Modules 207
Creating Interfaces 211
Creating Coclasses 216
Stand-Alone Attributes 219
Handling
Aggregation 221
Review of COM Aggregation and Delegation 221
The aggregatable and aggregates Attributes 223
Handling
Errors 224
Events
224
Adding Event Support 225
Handling Events 228
Compiler
Options 230
Generating IDL 231
Suppressing IDL Generation 231
7 ATL and ATL
Server 233
Changes
in ATL 7.0 233
New Module Classes 234
Data Handling and Collections 234
Shared Classes 243
The CSimpleStringT Class 244
The CStringT Class 246
String Conversion Classes 250
Security Classes 252
Regular Expression Classes 256
Other Changes 257
Breaking Changes Since Visual C++ 6.0 258
Introduction
to ATL Server 259
ATL Server
Architecture 259
ISAPI Extensions 260
Web Application DLLs 261
Server Response Files 261
Writing
Web Applications Using ATL Server 266
Using Attributes 266
Defining Handler Methods 268
A Sample ATL Server Application 270
Writing
Web Services Using ATL Server 277
ATL Server Web Service Architecture 277
Example: Creating a Web Service 281
Consuming
Web Services in C++ 283
PART III WRITING
COM+ CODE
8 A Simple COM+ Example
289
The Importance
of Using GUIDs 289
A Simple
Component Example 290
Creating the Simple Component 291
Registering the Component on the Server 293
Exporting the Application 295
Creating the Client 296
Testing the Application 298
A Component
Class Example 300
Deriving from the Component Class 300
Performing the Component Class Setup 305
Creating the Client 306
A ServicedComponent
Class Example 308
Deriving from the ServicedComponent Class 309
Performing the ServicedComponent Class Setup 310
Creating the Client 311
9 Working with Disconnected
Applications 315
Understanding
the Role of MSMQ in this Application 316
Creating
a Simple Recorder/Player 320
Defining the Message Queue 320
Accessing the Message Queue 322
Creating the Recorder 323
Testing the Recorder 324
Creating the Player 325
Creating
a Simple COM+ Listener/Player 327
Creating the Listener/Player Component 328
Defining the MSMQ Rule and Trigger 331
Testing the Listener/Player Application 334
Creating
the MSMQ Client Application 335
Designing the Data Encapsulation Component 336
Designing the Message Queue Component 337
Installing the Message Queue Component 339
Designing a Managed Client 341
Testing the Application 341
10 Creating Subscriptions
345
An Overview
of the COM+ Catalog 345
What
Are Subscriptions? 347
Understanding Transient Subscriptions 349
Understanding Permanent Subscriptions 349
Understanding the Need for Dynamic Registration 349
Creating
the Event Object 350
Designing the Component 350
Installing the Event Object 352
Creating
the Publisher 353
Creating
a Component Subscriber 355
Designing the Subscriber Component 355
Installing and Testing the Subscriber Component 357
Creating
a Dialog-Based Subscriber 360
Creating a Permanent Subscription 363
Testing the Permanent Subscription 368
Creating a Transient Subscription 369
Testing the Transient Subscription 372
11 Web Application
Scenarios 373
How Do
Web-Based Applications Differ? 374
COM+ 1.5 and SOAP 374
COM+ 1.5 and Application Dumps 380
Component Interactions 381
Scripting Error Handling 382
Human-Language Support 384
Accessibility Concerns 385
ASP and Component Communication 386
Defining
the Database 386
Creating
the Data Access Component 390
Using
ASP to Access the Database 393
Testing
the Application 396
PART IV INTEROPERABILITY
12 Interacting with
Unmanaged Code 401
Managed
and Unmanaged Code 402
Manual and Automatic Memory Management 403
Interoperating Between Managed and Unmanaged Code 403
Garbage Collection in .NET 404
Platform
Invoke 410
Using Platform Invoke from Visual Basic .NET 410
Converting Windows API Parameter Types 416
Using Platform Invoke from Visual C# 417
Using Platform Invoke from Managed C++ 421
Visual
C# Concerns 422
The unsafe Keyword 423
The fixed Keyword 425
The stackalloc Keyword 427
Visual
C++ Concerns 428
Marshaling Argument Types 428
Pinning 429
Calling Exported C++ Member Functions 430
IJW (It Just Works) 431
IJW vs. P/Invoke 431
13 Advanced
Interaction 435
The MarshalAs
Attribute 435
Marshaling
Strings 437
String and StringBuilder 439
Marshaling
Structs 440
The StructLayout Attribute 440
Handling Nested Structures '443
Marshaling
Arrays 447
Marshaling Arrays in Platform Invoke 447
Marshaling Arrays in COM Interop 451
Passing
Managed Pointers to Unmanaged Code 452
Pinning in Managed C++ 455
Using gcroot in Managed C++ 456
Dynamically
Loading Platform Invoke DLLs 458
Choosing the Path to the DLL at Run Time 458
Using
Callbacks 461
Introduction to Delegates 463
Using Delegates for Callbacks 464
Garbage
Collection Considerations 467
Using the KeepAlive Method 467
Using the HandleRef Type 468
Performance
Considerations 469
14 Working
with Predefined Interfaces 471
COM Requires
Specific Interfaces 471
Using
the OLE/COM Object Viewer 474
A Quick Overview of Interfaces 475
Viewing the .NET Category 477
Viewing an Unmanaged Control 480
Performing Interface Analysis 481
Re-creating
COM Interfaces Using Managed Code 483
Creating
a Component with Specialized Interfaces Example 487
INDEX 491 |