COM Interoperability

The code written for the .NET Framework is also referred to as managed code, and the code written for tradition Windows applications (previous to the .NET Framework) is called unmanaged code. Managed code contains metadata used by the Common Language Runtime (CLR).

The Component Object Model (COM) has been around for many years and is used by many Windows applications. Eventually the .NET platform will replace COM, but until then you may need to develop COM components from .NET and access them from unmanaged code, interoperability (also referred as COM interop) enables you to use existing COM components in .NET application or use .NET components in unmanaged code.

In COM, type libraries stored metadata for a COM component and described the characteristics of a COM component. In .NET an assembly is the primary building block if a .NET application. An assembly is a collection of functionality that is build, versioned, and deployed as a component. Similar to COM type libraries, an assembly contains an assembly manifest. The assembly manifest include information about an assembly such as the identity, version, culture, digital signature, compile-time dependencies, files that make up the assembly implementation, and permissions required to run the assembly properly.

In brief, COM understands the language of type libraries, and the .NET Framework understands the language of assembly manifests. So converting a type library into an assembly code, and converting an assembly manifest to a COM type library provides accessibility of .NET assemblies in unmanaged code.

Interop assemblies are .NET assemblies that act as a bridge between managed and unmanaged code. Internet assemblies provide mapping of COM object members to equivalent .NET managed members.

The .NET Framework defines a common set of data types. All .NET programming languages use these common data types. During the import process of a COM type library, there may be cases when the parameters and return values of COM objects use different data types than .NET data types. The interop marshaler handles these conversions for you. Interoperability marshaling is the process of packaging parameters and returning values into equivalent data types during conversion of a COM type library to an assembly manifest and an assembly manifest to a COM type library.

In the following sections, I’ll show you how to unmanaged libraries in managed code through COM interoperability.