Managed Extensions for C++ and Data Marshaling Tutorial
Managed Extensions for C++ can mix managed and unmanaged code and call traditional DLLs and COM objects directly. When working between unmanaged code and managed code written in different languages, however, the most common interoperability problems that you will encounter are marshaling problems. This tutorial provides code examples to show you how to use the following techniques to correctly and efficiently marshal data between managed and unmanaged code:
- Custom marshalers
- Managed wrappers around unmanaged types
In this tutorial, you will perform the following tasks:
- Generate an example DLL.
- Marshal blittable types.
- Marshal structures.
- Marshal arrays.
- Implement a custom marshaler for a managed type.
- Marshal function pointers.
- Use managed wrappers around unmanaged types.
All managed structure definitions and PInvoke signatures are defined in an assembly named PInvoke.dll. The wrapper class and custom marshaler for the native managed class are implemented in an assembly named TraditionalDLLWrapper.dll.
In This Section
- Discusses issues that arise when moving data between managed and unmanaged code or access data across the managed/unmanaged boundary.
- Example DLL
- Provides an initial view of the functions and data types exposed by a simple unmanaged DLL. This DLL is the basis for all the examples in this tutorial.
- Marshaling Blittable Types
- Describes how to use PInvoke to control the marshaling of blittable types.
- Marshaling Structures
- Describes how to use PInvoke to control the marshaling of managed structures types.
- Marshaling Arrays
- Describes how to use PInvoke to control the marshaling of arrays of blittable types and structures.
- Custom Marshaling
- Describes how to implement a custom marshaler for a managed type.
- Marshaling Function Pointers
- Describes how to use PInvoke to interoperate with unmanaged functions that require a function pointer.
- Managed Wrappers Around Unmanaged Types
- Describes how to implement a managed wrapper class that encapsulates unmanaged functions and data types.
- Provides advanced tutorials on using Managed Extensions for C++ and interoperating with managed and unmanaged code.
- Adding Functionality
- Provides links to topics discussing how to write code with Managed Extensions.
- Provides links to reference material on keywords, pragmas, compiler and linker options, attributes, and preprocessor directives.
- Provides links to samples that show how to use Managed Extensions to write .NET Framework applications.