Share via


General Considerations

All calls made between managed and unmanaged code must negotiate the requirements imposed by each 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
Coding model Interface-based Object-based
Identity GUIDs Strong names
Error handling mechanism HRESULTs Exceptions
Type compatibility Binary standard Type standard
Type definition Type library Metadata
Type safety Not type safe Optionally safe
Versioning Immutable Resilient

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 Globally Unique Identifiers (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.

The following sections explain each characteristic in more detail.

  • Coding models
    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.

  • Error handling mechanisms
    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.

  • Identities
    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.

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

  • Type definitions
    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
    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 due to its unsafe behavior. Interoperation services prevent untrusted, managed code from accessing unmanaged code.

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

See Also

Design Considerations for Interoperation