Share via


Design Considerations for Interoperation

This overview explains the differences between the managed and unmanaged programming models. For recommendations and design-time interoperation strategies, see Building COM Components for Interoperation and Building .NET Framework Components for Interoperation.

All calls made between managed and unmanaged code must negotiate the requirements imposed by the respective programming model. Managed and unmanaged programming models are dissimilar in many respects. The following table shows the defining characteristics of each model.

Characteristic

Unmanaged model

Managed model

Details

Coding model

Interface-based

Object-based

Unmanaged objects always communicate through interfaces; managed objects and classes can pass data directly without implementing interfaces.

By default, COM interop generates a class interface to expose managed functionality through an interface to COM when the object or class does not implement one.

Identity

GUIDs

Strong names

GUIDs identify a specific unmanaged type and provide no location information for that type. Strong names consist of a unique assembly name in addition to a type name. Because the assembly name uniquely identifies the type, you can reuse a type name across multiple assemblies. An assembly also introduces publisher key, version, and location information to a managed type. Interoperation services generate GUIDs and are strong-named as required.

Error handling mechanism

HRESULTs

Exceptions

COM methods usually return an HRESULT, indicating that the call succeeded or failed. Managed code incorporates exceptions. By default, COM interop maps managed exceptions to failure HRESULTs.

Plain old data structures (PODS)

Structure

Managed structure derived from object

Platform invoke cannot be used to return structures or classes by value if they contain a constructor. In general, user-defined types that contain non-PODS elements should be returned by reference. PODS are data structures that contain only passive collections of field values, as defined by the ISO/IEC standard 14882, "Programming Languages — C++." They do not contain constructors, copy assignment operators, destructors, or non-static data members that are not themselves PODS.

Type compatibility

Binary standard

Type standard

Types vary between managed and unmanaged code, and also among languages.

Type definition

Type library

Metadata

If you are accustomed to working with type libraries, you know that they contain only public types. Moreover, a type library is optional. In the managed programming model, type information is mandatory for all types. Interoperation services provide tools that convert type libraries to metadata in assemblies and metadata to type libraries.

Type safety

Not type safe

Optionally safe

Unmanaged compilers provide no type checking on pointer types, making the code susceptible to potentially harmful activity. In general, managed code requires a higher level of trust. Programmers can continue to use pointers in managed code, although the code has restrictions because of its unsafe behavior. Interoperation services prevent untrusted, managed code from accessing unmanaged code.

Versioning

Immutable

Resilient

COM interfaces are immutable. If you change an interface, you must rename it with a new GUID. Managed types can evolve while keeping the same name.

Some programming model characteristics have associated entities that you can view, such as type libraries and metadata. Some you can pass as arguments, such as GUIDs and strong names. Other characteristics are more conceptual; you will undoubtedly consider their impact on your application design, but you will not encounter direct mapping between the managed and unmanaged models for these characteristics.

Title

Description

Building COM Components for Interoperation

Describes design-time interoperation strategies for COM components.

Building .NET Framework Components for Interoperation

Describes design-time interoperation strategies for .NET Framework components.

Interoperating with Unmanaged Code

Describes how to use COM interop and platform invoke.

Advanced COM Interoperability

Describes COM interop concepts and conversion rules.

Interop Marshaling

Describes the interop marshaling service, its relationship to COM marshaling, and its role in remote communications.