Skip to main content

Interoperability

How does a .NET application connect to unmanaged code, including COM libraries, ActiveX controls, and native (Win32) DLLs? This section includes links to resources to help you connect your .NET code to the unmanaged component(s) that you need to use. Interoperability classes include:

NamespaceDescription
System.Runtime.InteropServicesProvides a wide variety of members that support COM interop and platform invoke services.
System.Runtime.InteropServices.CustomMarshalersSupports the .NET infrastructure and is not intended to be used directly from your code.
System.Runtime.InteropServices.ExpandoContains the IExpando interface which allows modification of an object by adding or removing its members.

Essentials

An Overview of Managed/Unmanaged Code Interoperability
This article provides basic facts about interoperability between managed and unmanaged code, and guidelines and common practices for accessing and wrapping unmanaged API from managed code, and for exposing managed APIs to unmanaged callers. Security and reliability considerations, performance data, and general practices for development processes are also highlighted.
Platform Invoke Tutorial
This tutorial shows you what you need to do to be able to call unmanaged DLL functions from C#. The attributes discussed in the tutorial allow you to call these functions and have data types be marshaled correctly.
Microsoft Win32 to Microsoft .NET Framework API Map
This article identifies the Microsoft .NET Framework version 1.0 or 1.1 APIs that provide similar functionality to Microsoft Win32 functions. One or more relevant .NET Framework APIs are shown for each Win32 function listed. The intended audience for this article is experienced Win32 developers who are creating applications or libraries based on the Microsoft .NET Framework, but anyone looking for a managed counterpart for a Win32 function could find this document useful.
Default Marshaling for Value Types

Most value types, such as integers and floating-point numbers, are blittable and do not require marshaling. Other non-blittable types have dissimilar representations in managed and unmanaged memory and do require marshaling. Still other types require explicit formatting across the interoperation boundary.

This topic provides the follow information on formatted value types:

StructLayoutAttribute
Passing Structures
Many unmanaged functions expect you to pass, as a parameter to the function, members of structures (user-defined types in Visual Basic) or members of classes that are defined in managed code. When passing structures or classes to unmanaged code using platform invoke, you must provide additional information to preserve the original layout and alignment. This topic introduces the StructLayoutAttribute attribute, which you use to define formatted types. For managed structures and classes, you can select from several predictable layout behaviors supplied by the LayoutKind enumeration.
Marshaling Data With Platform Invoke
This section provides samples that demonstrate how to create managed function prototypes for passing arguments to and receiving return values from functions exported by unmanaged libraries. The samples also demonstrate when to use the MarshalAsAttribute attribute and the Marshal class to explicitly marshal data.
Calling Win32 DLLs in C# with P/Invoke
This column focuses on the practical use and application of P\Invoke for calling Windows API functions.
Use P/Invoke to Develop a .NET Library for Serial Device Communications
Out-of-the-box, the only way of coding RS232 serial communications applications in the .NET environment is to import the outdated and somewhat limited MSComm ActiveX control. This article describes the development of a lean, multithreaded, and modern RS232 base class library in C# managed code. The library uses Platform Invocation Services to interact with the Win32 API directly. Application programmers can use the library from any .NET language through inheritance; the article explores examples written in C# and Visual Basic .NET.
Beyond (COM) Add Reference: Has Anyone Seen the Bridge?
Discover why bridges between COM Interop and the Microsoft .NET Framework are needed, and how they are implemented.
Using the .NET Framework SDK Interoperability Tools
Get the most out of the .NET Framework SDK interoperability tools, including advanced uses of TLBIMP, SN, TLBEXP, and REGASM. To go beyond (COM) Add Reference, you need to understand these tools.

Compact Framework

An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework
Learn how to use the Platform Invoke (P/Invoke) feature of the .NET Compact Framework.
Advanced P/Invoke on the Microsoft .NET Compact Framework
Explore advanced interoperability on the .NET Compact Framework, including now to marshall complex types and strings in structures.
Marshalling Types During Platform Invoke (P/Invoke) on the Microsoft .NET Compact Framework
Learn how to marshal data between managed and unmanaged code using the .NET Compact Framework.
Creating a P/Invoke Library
The intent of this sample is to provide a collection of P\Invokes, as well test procedures that demonstrate their application. This sample provides several of the most commonly requested P\Invoke functions as well as a class that handles the enumeration and invoking of the various test procedures. This class is used by a Form to allow the user to select and run the tests from a selection list.
P/Invoke Serial APIs in the Compact Framework
This article considers specific challenges the .NET Compact Framework presents when controlling serial communications from managed code. Design principles, portable code, and implementation of P\Invoking are discussed.
Asynchronous Callbacks from Native Win32 Code
In the .NET Compact Framework newsgroup several people have asked questions about passing delegates to native Win32 DLL's to be used as asynchronous callbacks to P\Invoke back into managed code after an unmanaged function wants to report back to its caller. Unfortunately, passing delegates to unmanaged code is not supported by the .NET Compact Framework. As an alternative, the .NET Compact Framework contains the MessageWindow class. This class can be used to pass Windows messages from unmanaged code to managed code. The functionality of MessageWindow is great if you need to pass Windows messages to a managed control. In certain solutions however, using the MessageWindow class is somewhat artificial and not a true replacement for delegates. With a little extra coding effort it is possible to setup an event handler mechanism that exposes itself to the managed application developer as if true delegates exist to call back from unmanaged code into managed code. In this article we provide you with the details of this mechanism.

Cryptography

Extend .NET Cryptography with CAPICOM and P/Invoke
How to supplement the cryptographic capabilities currently shipping with the Microsoft .NET Framework version 1.1 through the use of COM interop and P\Invoke to CryptoAPI native libraries.
Replacing API Calls with .NET Framework Classes
Discover replacements for existing Win32 API calls in the Microsoft .NET Framework to accomplish the same goals you may have sought with one or more calls using Visual Basic 6.0.
Excel, Exchange, and C#
Eric Gunnerson shows you how to use Outlook, Excel, and C# to create a custom calendar that provides an easy-to-read layout for both short- and long-term projects.
SOS: It`s Not Just an ABBA Song Anymore
Son of Strike (SOS) is a mixed-mode debugging tool, written as a WinDBG extension, that allows you to get information from both the managed and native sides of an application.
Migrating Native Code to the .NET Common Language Runtime
Find out how the Microsoft .NET runtime allows a native Win32-based, C, C++, Visual Basic 6.0-based, or COM code base to coexist with managed components written in .NET-friendly languages.
Get Ready for Microsoft .NET by Using Wrappers to Interact with COM-based Applications
Defines wrappers and discusses other ways for .NET components to take part in COM+ transactions. As part of a tutorial on the interoperation of COM and .NET, the article discusses how ActiveX containers can host .NET controls, and how .NET containers can host ActiveX controls.
Windows Hooks in the .NET Framework
Dino Esposito tackles Windows hooks from the perspective of .NET applications and discuss how to build .NET Framework wrapper classes for them.
Primary Interop Assemblies (PIAs)
Learn about the process of creating metadata that describes a set of existing COM types to interoperate with the Microsoft .NET Framework common language runtime.
Calling COM Components from .NET-based Clients
Delivers specific techniques that can serve as an important part of your migration strategy when you're starting to move your development from Visual Basic to Visual Basic .NET. Particular attention is paid to the ability to call COM components from .NET-based code.
Calling a .NET Component from a COM Component
Offers essential techniques on calling Microsoft Windows 2003 servers from COM clients to show you how to migrate a complex application from COM to .NET one component at a time.
Understanding Enterprise Services (COM+) in .NET
Get the technical details behind the integration of Microsoft .NET and COM+ Services, as well as the services available to managed code. 
Using COM+ Services in Microsoft .NET
Add new .NET-based components to existing COM and COM+ applications and they can work together to help you develop .NET-based applications that can participate in transactions, take advantage of role-based security, or interact with a queue.
Microsoft .NET and Windows XP COM+ Integration with SOAP
Discover how to expose existing COM+ applications over SOAP using Microsoft Windows XP and the .NET Framework. An accompanying downloadable application helps you follow along by performing the same actions on your machine.
System.Management Lets You Take Advantage of WMI APIs within Managed Code
After presenting an overview of what's new for WMI in the .NET Framework and the Visual Studio .NET environment, the author provides an in-depth exploration of the Management Extensions in Visual Studio .NET for Server Explorer.