Share via


Interoperability (C# Programming Guide) 

Interoperability enables you to preserve and leverage existing investments in unmanaged code. Code running under the control of the common language runtime (CLR) is called "managed code", and code running outside the CLR is "unmanaged code". COM, COM+, C++ components, ActiveX components, and Win32 API are examples of unmanaged code.

The .NET Framework enables interoperability with unmanaged code through platform invoke services, the System.Runtime.InteropServices namespace, and the CLR and through COM Interoperability (COM interop).

  • There are two ways to use unmanaged APIs from managed code: through platform invoke and through It Just Works (IJW) in C++. Platform invoke enables managed code to call functions exported from an unmanaged dynamic link library (DLL), such as Win32 API and custom DLLs. The CLR handles DLL loading and parameter marshaling. For performance, check if there is an equivalent function available in the .NET Framework rather than using platform invoke. For more information, see A Closer Look at Platform Invoke.

  • COM interop, which enables managed code to interact with COM objects through COM interfaces and COM clients. There are two ways to use COM components from managed code:

    • For calling OLE Automation compatible COM components, use COM interop or tlbimp.exe. The CLR handles COM component activation and parameter marshaling.

    • For IDL based COM components, use IJW and C++. Every public managed class that implements IUnknown, IDispatch, and other standard COM interfaces can be called from unmanaged code through COM interop. For more information, see Microsoft .NET/COM Migration and Interoperability.

For more information, see Interoperating with Unmanaged Code and Improving Interop Performance.

Both PInvoke and COM interop use marshaling to translate arguments such as integers, strings, arrays, structures, and pointers between managed and unmanaged code. For more information, see Interop Marshaling Overview.

Interoperability Overview (C# Programming Guide)

How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide)

How to: Use Platform Invoke to Play a Wave File (C# Programming Guide)

How to: Use COM Interop to Check Spelling Using Word (C# Programming Guide)

Example COM Class (C# Programming Guide)

How to: Use Managed Code as an Automation Add-In for Excel (C# Programming Guide)

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 10.5.7 External methods

  • 17.5 Attributes for Interoperation

  • 18.8 Dynamic memory allocation

See Also

Concepts

C# Programming Guide

Other Resources

Interoperability in the .NET Compact Framework