Compartilhar via


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

  • PinvokeLib.dll
    Provides code that defines the library functions provided by Pinvoke.dll.
  • Marshaling Strings
    Describes how to pass strings by value, by reference, in structures, in classes, and in arrays.
  • Marshaling Arrays of Types
    Describes how to pass multidimensional arrays of integers by value and how to pass one-dimensional arrays by reference.