.NET framework community website  
 
Search gotdotnet for:
Platform Invoke Cheat Sheet

Base Class Libraries Introduction

We recognize that using P\Invoke can sometimes be harder than you might feel is necessary. There are many nuances to how you define your signatures and structures, and how to marshal types. There are many resources available on MSDN and in the SDK, but they are often hard to find and not especially well cross-referenced. To help ease the burden on .Net developers we have compiled a list of resources that should provide assistance to any developer, beginner or advanced, having problems with a wide range of P\Invoke problems.

P\Invoke Signature and Structure Samples
View the ReadMe describing the platform invoke samples demonstrating how to call functions exported from an unmanaged library: how to declare different types, how to use available attributes to modify default behavior, how to use methods of the Marshal class when needed, and which aspects of garbage collection and threading could affect results.
Zip File containing samples described above

P\Invoke Signature and Structure Library for Win32
When a Win32 API is not exposed by the .NET framework you must manually write an interop wrapper to access that API. Writing these wrappers is difficult and error prone. This post includes C# definitions of many common Win32APIs and their related data structures. It does not include all Win32 APIs and it is missing all #define constants. It was created by a rather complex semi-automatic process. These definitions are all unsafe C# and you should understand the implications of this before using the definitions. It should also be noted that these methods are defined with the least common type that is guaranteed to work and you may not be able to tell exactly which data-type the api requires where just by looking at the signatures, be sure to reference the Win32 API for specifications on these API calls.


MSDN Articles and Tutorials

General

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:

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 API”s from C#

This column focuses on the practical use and application of P\Invoke for calling Windows API functions.

Use P\Invoke to Develop a .NET Base Class 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.

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.

Marshalling Types During Platform 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.

PInvoking Serial APIS in Compact Framework

This paper 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

Extending .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.



[Printer friendly version]
© Copyright 2007 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | About the team | Contact Us