Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Developer's Guide 
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.

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 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 Sections

Interop Marshaling

Describes marshaling for COM interop and platform invoke.

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Interoping with Native Libraries      Zac Bowling - Mono   |   Edit   |   Show History

The guide to interoping with native libraries created by the Mono team is an awesome resource of information on the topic for both Microsoft.NET and Mono.

http://www.mono-project.com/Interop_with_Native_Libraries

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker