1. Introduction to the .NET Framework
Features of the .NET Framework
Version Resiliency (The End of DLL Hell)
Concepts Important for This Book
2. Bridging the Two Worlds—Managed and Unmanaged Code
Managed Code Versus Unmanaged Code
Goals of Unmanaged Code Interaction
How Can Using Unmanaged Code Be Secure?
How Unmanaged Code Interacts with Managed Code
Platform Invocation Services (PInvoke)
Mixed-Mode Programming Using Managed Extensions to C++
Java User Migration Path to .NET (JUMP to .NET)
Unmanaged Code Isn’t Always the Answer
Part II. Using COM Components in .NET Applications
3. The Essentials for Using COM in Managed Code
Referencing a COM Component in Visual Studio .NET
Referencing a COM Component Using Only the .NET Framework SDK
Example: A Spoken Hello, World
Using the Microsoft Speech API
Using COM Objects in ASP.NET Pages
An Introduction to Interop Marshaling
Common Interactions with COM Objects
Calling Methods and Properties on a COM Object
Casting to an Interface (QueryInterface
)
Passing the Right Type of Object
Late Binding and By-Reference Parameters
Using ActiveX Controls in .NET Applications
Referencing an ActiveX Control in Visual Studio .NET
Referencing an ActiveX Control Using Only the .NET Framework SDK
Deploying a .NET Application That Uses COM
Example: Using Microsoft Word to Check Spelling
4. An In-Depth Look at Imported Assemblies
Combining Several Types in One
Converting Methods and Properties
Coclass Interfaces and Parameter/Field Replacement
Handling COM Events in Managed Code
Type Library Importer Transformations
Lazy Connection Point Initialization
Connectable Objects You Don’t Instantiate
Handling ActiveX Control Events in Managed Code
ActiveX Importer Transformations
6. Advanced Topics for Using COM Components
C# Unsafe Code Versus System.Runtime.InteropServices
APIs
Examples of Manipulating IntPtr
Types
Threading in COM Versus Threading in .NET
Choosing Your Apartment State in a .NET Application
Callbacks from a COM Object to a .NET Object
Troubleshooting an InvalidCastException
7. Modifying Interop Assemblies
How to Change an Assembly’s Contents
Exposing SAFEARRAY
s Differently
Adding Size Information to C-Style Arrays
Marking Classes as Visual Basic Modules
Adding Back helpstring
Information
Adding Back IDL Custom Attributes
Part III. Using .NET Components in COM Applications
8. The Essentials for Using .NET Components from COM
Using a .NET Component in Visual Basic 6
Using a .NET Component in Unmanaged Visual C++
Using a .NET Component in Unmanaged JScript
Interacting with a .NET Object
Calling Members on a .NET Object
Getting Rich Error Information
Deploying a COM Application That Uses .NET
Hosting Windows Forms Controls in Internet Explorer
9. An In-Depth Look at Exported Type Libraries
10. Advanced Topics for Using .NET Components
Hosting the Common Language Runtime
Using the ClrCreateManagedInstance
API
Hosting Windows Forms Controls in Any ActiveX Container
Working Around COM-Invisibility
Using Reflection to Invoke Static Members
Unexpected Casing in Type Libraries
Part IV. Designing Great .NET Components for COM Clients
11. .NET Design Guidelines for Components Used by COM Clients
Interfaces Versus Custom Attributes
Testing Your Component from COM
12. Customizing COM’s View of .NET Components
Detecting Incorrect Use of MarshalAsAttribute
Using Visual Basic .NET’s ComClassAttribute
Adding Arbitrary Registration Code
Controlling Interface Derivation
Disabling Type Library Marshaling of .NET Interfaces
Creating Multi-Cultured Methods
Using Optional Parameters in Any Language
Exposing .NET Objects As COM+ Objects
13. Exposing .NET Events to COM Clients
Exposing Events Without Using Extra CLR Support
Exposing Events Using Extra CLR Support
Using ComSourceInterfacesAttribute
Visual Basic .NET’s ComClassAttribute
Example: Handling a .NET Windows Form’s Events from COM
14. Implementing COM Interfaces for Binary Compatibility
Binary Compatibility with Visual Basic 6 Classes
Example: Implementing Office XP Smart Tag Interfaces
Running the Example Using Visual Studio .NET
Running the Example Using Only the .NET Framework SDK
Interface Implementation Shortcuts in Visual Studio .NET
Common Problems When Implementing COM Interfaces
COM Interfaces with Default CCW Implementations
COM Interfaces Bridged to Different .NET Types
Part V. Designing Great COM Components for .NET Clients
15. Creating and Deploying Useful Primary Interop Assemblies
Primary Interop Assembly or Brand New Assembly?
Creating a Primary Interop Assembly
Handling References to Other Type Libraries
Deploying and Registering a Primary Interop Assembly
Writing IDL That Produces Good Type Libraries
Defining Structures, Enums, and Unions
16. COM Design Guidelines for Components Used by .NET Clients
Issues with VARIANT
Parameters
Reserve Failure HRESULT
s for Exceptional Circumstances
Don’t Return Success HRESULT
s Other than S_OK
Set Additional Error Information
Adjusting Certain COM-Specific Idioms
Threading and Apartment Guidelines
Providing Self-Describing Type Information
17. Implementing .NET Interfaces for Type Compatibility
Considerations for Visual C++ Programmers
Example: Implementing IDisposable
to Clean Up Resources
Considerations for Visual Basic 6 Programmers
Example: Implementing IFormattable
to Customize ToString
Example: Implementing IHashCodeProvider
and IComparer
to Use a COM Object as a Hashtable Key
Part VI. Platform Invocation Services (PInvoke)
Using PInvoke in Visual Basic .NET
Using PInvoke in Other .NET Languages
Choosing the Right Parameter Types
Customizing Declare
and DllImportAttribute
Choosing a Different Function Name
Customizing the Behavior of Strings
Changing the “Exact Spelling” Setting
19. Deeper Into PInvoke and Useful Examples
Using Delegates as Function Pointers
Invoking Unmanaged Function Pointers in Managed Code
Customizing Structures with Custom Attributes
Handling Variable-Length Structures and Signatures
Guarding Against Premature Garbage Collection
The System.GC.KeepAlive
Method
The System.Runtime.InteropServices.HandleRef
Value Type
Choosing the DLL Location or Name Dynamically
Example: Responding Immediately to Console Input
Example: Clearing the Console Screen
Example: Using CoCreateInstanceEx
to Activate Remote COM Objects
Transforming Types Without Custom Marshaling
Custom Marshaling Architecture
Marshalers, Marshalers, Marshalers!
Example: Marshaling Between .NET and COM Fonts
Example: Marshaling Between System.IO.Stream
and IStream
Example: Marshaling With Arrays
Example: Providing Deterministic Release of Resources
21. Manually Defining COM Types in Source Code
Manually Defining COM Interfaces
Working With Language Limitations
Manually Defining Coclass Interfaces and Event Types
Manually Defining COM Structures
Defining Classes the Simple Way
22. Using APIs Instead of SDK Tools
Generating an Assembly from a Type Library
Getting a Primary Interop Assembly
Generating a Type Library from an Assembly
Creating a Dynamic Type Library
Saving the Dynamic Type Library
Registering and Unregistering Assemblies
Installing and Uninstalling Serviced Components
Example: Using the APIs in an Interactive Application
Part VIII. Comprehensive Examples
23. Writing a .NET Arcade Game Using DirectX
E-mail Attack—The Advanced Version
24. Writing .NET Visualizations For Windows Media Player
A. System.Runtime.InteropServices
Reference
The System.Runtime.InteropServices
Namespace
The ArrayWithOffset
Value Type
The AssemblyRegistrationFlags
Enumeration
The AutomationProxyAttribute
Custom Attribute
The CallingConvention
Enumeration
The ClassInterfaceAttribute
Custom Attribute
The ClassInterfaceType
Enumeration
The CoClassAttribute
Custom Attribute
The ComAliasNameAttribute
Custom Attribute
The ComConversionLossAttribute
Custom Attribute
The ComEventInterfaceAttribute
Custom Attribute
The ComImportAttribute
Pseudo-Custom Attribute
The ComInterfaceType
Enumeration
The ComRegisterFunctionAttribute
Custom Attribute
The ComSourceInterfacesAttribute
Custom Attribute
The ComUnregisterFunctionAttribute
Custom Attribute
The ComVisibleAttribute
Custom Attribute
The DispIdAttribute
Custom Attribute
The DllImportAttribute
Pseudo-Custom Attribute
The ExporterEventKind
Enumeration
The ExtensibleClassFactory
Class
The ExternalException
Exception
The FieldOffsetAttribute
Pseudo-Custom Attribute
The GuidAttribute
Custom Attribute
The ICustomMarshaler
Interface
The IDispatchImplAttribute
Custom Attribute
The IDispatchImplType
Enumeration
The ImportedFromTypeLibAttribute
Custom Attribute
The ImporterEventKind
Enumeration
The InAttribute
Pseudo-Custom Attribute
The InterfaceTypeAttribute
Custom Attribute
The InvalidComObjectException
Exception
The InvalidOleVariantTypeException
Exception
The IRegistrationServices
Interface
The ITypeLibConverter
Interface
The ITypeLibExporterNameProvider
Interface
The ITypeLibExporterNotifySink
Interface
The ITypeLibImporterNotifySink
Interface
The LCIDConversionAttribute
Custom Attribute
The MarshalAsAttribute
Pseudo-Custom Attribute
The MarshalDirectiveException
Exception
The ObjectCreationDelegate
Delegate
The OptionalAttribute
Pseudo-Custom Attribute
The OutAttribute
Pseudo-Custom Attribute
The PreserveSigAttribute
Pseudo-Custom Attribute
The PrimaryInteropAssemblyAttribute
Custom Attribute
The ProgIdAttribute
Custom Attribute
The RegistrationServices
Class
The SafeArrayRankMismatchException
Exception
The SafeArrayTypeMismatchException
Exception
The StructLayoutAttribute
Pseudo-Custom Attribute
The TypeLibExporterFlags
Enumeration
The TypeLibFuncAttribute
Custom Attribute
The TypeLibFuncFlags
Enumeration
The TypeLibImporterFlags
Enumeration
The TypeLibTypeAttribute
Custom Attribute
The TypeLibTypeFlags
Enumeration
The TypeLibVarAttribute
Custom Attribute
The TypeLibVarFlags
Enumeration
The UCOMIConnectionPoint
Interface
The UCOMIConnectionPointContainer
Interface
The UCOMIEnumConnectionPoints
Interface
The UCOMIEnumConnections
Interface
The UCOMIEnumMoniker
Interface
The UCOMIEnumVARIANT
Interface
The UCOMIPersistFile
Interface
The UCOMIRunningObjectTable
Interface
The System.Runtime.InteropServices.CustomMarshalers
Namespace
The EnumerableToDispatchMarshaler
Class
The EnumeratorToEnumVariantMarshaler
Class
The ExpandoToDispatchExMarshaler
Class
The TypeToTypeInfoMarshaler
Class
The System.Runtime.InteropServices.Expando
Namespace
C. HRESULT
to .NET Exception Transformations
D. .NET Exception to HRESULT
Transformations
52.15.135.63