Marshaling Data with Platform Invoke
To call functions exported from an unmanaged library, a .NET Framework application requires a function prototype in managed code that represents the unmanaged function. To create a prototype that enables platform invoke to marshal data correctly, you must do the following:
- Apply the DLLImportAttribute attribute to the static function or method in managed code.
- Substitute managed data types for unmanaged data types.
You can use the documentation supplied with an unmanaged function to construct an equivalent managed prototype by applying the attribute with its optional fields and substituting managed data types for unmanaged types. For instructions about how to apply the DllImportAttribute, see Consuming Unmanaged DLL Functions.
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.
In This Section
- Platform Invoke Data Types
Provides a list of managed data types and their corresponding unmanaged data types. - Marshaling Strings
Describes how to pass strings by value, by reference, in structures, in classes, and in arrays. - Marshaling Classes, Structures, and Unions
Describes how to pass classes by value, how to pass various structures, and how to pass unions with value and mixed types. - Marshaling Arrays of Types
Describes how to pass multidimensional arrays of integers by value and how to pass one-dimensional arrays by reference. - Miscellaneous Marshaling Samples
Describes aspects of garbage collection and threading that affect interop marshaling behavior.
Related Topics
- Interop Marshaling
Describes how the runtime marshals data to and from unmanaged code. - Default Marshaling Behavior
Describes the rules that the interop marshaling service uses to marshal data. - Consuming Unmanaged DLL Functions
Describes how to call unmanaged DLL functions using platform invoke.