Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions docs/framework/interop/marshalling-data-with-platform-invoke.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
---
title: "Marshalling Data with Platform Invoke"
description: Marshal data with platform invoke in .NET. See a list of data types used in Windows APIs and C-style functions, and find their .NET managed type equivalents.
ms.date: "03/20/2019"
ms.date: "03/06/2026"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "platform invoke, marshalling data"
- "data marshalling, platform invoke"
- "marshaling, platform invoke"
ms.assetid: dc5c76cf-7b12-406f-b79c-d1a023ec245d
ai-usage: ai-assisted
---
# Marshalling Data with Platform Invoke

Expand Down Expand Up @@ -52,25 +55,29 @@ For corresponding types in Visual Basic, C#, and C++, see the [Introduction to t

## PinvokeLib.dll

The following code defines the library functions provided by Pinvoke.dll. Many samples described in this section call this library.
The following code defines the library functions provided by PInvokeLib.dll. Many samples described in this section call this library.

### Example

[!code-cpp[PInvokeLib#1](../../../samples/snippets/cpp/VS_Snippets_CLR/pinvokelib/cpp/pinvokelib.cpp#1)]

[!code-cpp[PInvokeLib#2](../../../samples/snippets/cpp/VS_Snippets_CLR/pinvokelib/cpp/pinvokelib.h#2)]

To call the library functions from managed code, first implement the managed prototypes for each function you want to invoke.
If the unmanaged code uses any custom types, you must also declare those types in your managed code.
Decorate the prototype with the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute.
To call the library functions from managed code, implement managed prototypes for each function you want to invoke. If the unmanaged code uses any custom types, you must also declare those types in your managed code. Decorate each prototype with the <xref:System.Runtime.InteropServices.DllImportAttribute> attribute, and use <xref:System.Runtime.InteropServices.StructLayoutAttribute> to control the layout of managed structures so they match the unmanaged equivalents.

The following code shows an example prototype:
### Managed type declarations

```csharp
// Managed prototype for TestingStructInStruct, which is declared and defined in an unmanaged library.
[DllImport("..\\LIB\\PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestStructInStruct(ref MyPerson2 person2);
```
The following code shows the managed equivalents of the unmanaged types defined in `PInvokeLib.h`. Each structure uses `StructLayout` to ensure the field layout matches the unmanaged layout:

:::code language="csharp" source="./snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/NativeMethods.cs" id="ManagedTypes":::
:::code language="vb" source="./snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/NativeMethods.vb" id="ManagedTypes":::

### Managed function prototypes

The following code shows the `DllImport` declarations that expose the unmanaged functions from `PinvokeLib.dll` to managed code:

:::code language="csharp" source="./snippets/marshalling-data-with-platform-invoke/csharp/PInvokeLibManaged/NativeMethods.cs" id="NativeMethods":::
:::code language="vb" source="./snippets/marshalling-data-with-platform-invoke/vb/PInvokeLibManaged/NativeMethods.vb" id="NativeMethods":::

For more information and examples, see the following articles:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Runtime.InteropServices;

// <ManagedTypes>
// Managed type declarations that correspond to the unmanaged types in PinvokeLib.dll.

[StructLayout(LayoutKind.Sequential)]
internal struct MyPoint
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct MyPerson
{
public string first;
public string last;
}

[StructLayout(LayoutKind.Sequential)]
internal struct MyPerson2
{
public IntPtr person; // Pointer to a MyPerson structure
public int age;
}

[StructLayout(LayoutKind.Sequential)]
internal struct MyPerson3
{
public MyPerson person; // Embedded MyPerson structure
public int age;
}

[StructLayout(LayoutKind.Explicit)]
internal struct MyUnion
{
[FieldOffset(0)] public int i;
[FieldOffset(0)] public double d;
}

[StructLayout(LayoutKind.Sequential)]
internal struct MyArrayStruct
{
public bool flag;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}
// </ManagedTypes>

// <NativeMethods>
internal static class NativeMethods
{
[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestArrayOfInts(
[In, Out] int[] array, int size);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestRefArrayOfInts(
ref IntPtr array, ref int size);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestMatrixOfInts(
[In, Out] int[,] matrix, int row);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestArrayOfStrings(
[In, Out] string[] array, int size);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestArrayOfStructs(
[In, Out] MyPoint[] pointArray, int size);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestArrayOfStructs2(
[In, Out] MyPerson[] personArray, int size);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TestStructInStruct(ref MyPerson2 person2);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern void TestStructInStruct3(MyPerson3 person3);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern void TestUnion(MyUnion u, int type);

[DllImport("PinvokeLib.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern void TestArrayInStruct(ref MyArrayStruct myStruct);
}
// </NativeMethods>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Console.WriteLine("Hello, World!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Imports System.Runtime.InteropServices

' <ManagedTypes>
' Managed type declarations that correspond to the unmanaged types in PinvokeLib.dll.

<StructLayout(LayoutKind.Sequential)>
Friend Structure MyPoint
Public x As Integer
Public y As Integer
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Friend Structure MyPerson
Public first As String
Public last As String
End Structure

<StructLayout(LayoutKind.Sequential)>
Friend Structure MyPerson2
Public person As IntPtr ' Pointer to a MyPerson structure
Public age As Integer
End Structure

<StructLayout(LayoutKind.Sequential)>
Friend Structure MyPerson3
Public person As MyPerson ' Embedded MyPerson structure
Public age As Integer
End Structure

<StructLayout(LayoutKind.Explicit)>
Friend Structure MyUnion
<FieldOffset(0)> Public i As Integer
<FieldOffset(0)> Public d As Double
End Structure

<StructLayout(LayoutKind.Sequential)>
Friend Structure MyArrayStruct
Public flag As Boolean

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)>
Public vals As Integer()
End Structure
' </ManagedTypes>

' <NativeMethods>
Friend Class NativeMethods
<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestArrayOfInts(
<[In], Out> ByVal array() As Integer, ByVal size As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestRefArrayOfInts(
ByRef array As IntPtr, ByRef size As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestMatrixOfInts(
<[In], Out> ByVal matrix(,) As Integer, ByVal row As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestArrayOfStrings(
<[In], Out> ByVal array() As String, ByVal size As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestArrayOfStructs(
<[In], Out> ByVal pointArray() As MyPoint, ByVal size As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestArrayOfStructs2(
<[In], Out> ByVal personArray() As MyPerson, ByVal size As Integer) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Function TestStructInStruct(
ByRef person2 As MyPerson2) As Integer
End Function

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Sub TestStructInStruct3(ByVal person3 As MyPerson3)
End Sub

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Sub TestUnion(ByVal u As MyUnion, ByVal type As Integer)
End Sub

<DllImport("PinvokeLib.dll", CallingConvention:=CallingConvention.Cdecl)>
Friend Shared Sub TestArrayInStruct(ByRef myStruct As MyArrayStruct)
End Sub
End Class
' </NativeMethods>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>PInvokeLibManaged</RootNamespace>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Imports System

Module Program
Sub Main(args As String())
Console.WriteLine("Hello World!")
End Sub
End Module