Blittable and Non-Blittable Types in C# and C++ Platform Invocation (P/Invoke)

In C#, there are certain types that can be used directly between managed and unmanaged code without any conversion; these types are referred to as blittable types.

The types are as follows:

System.Byte System.SByte System.Int16 System.UInt16
System.Int32 System.UInt32 System.Int64 System.UInt64
System.IntPtr System.UIntPtr System.Single System.Double

There are also two special kinds of blittable types.

1. One-dimensional blittable type arrays, where the type containing the array of blittable type variables is not itself a blittable type.

2. All formatted value types that only contain blittable types (as well as classes marshaled as formatted types).

Refer to (https://docs.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-behavior#default-marshaling-for-value-types)

Object references belong to non-blittable types.

When performing platform invocation with non-blittable types, these non-blittable data types must be converted into a marshalable form, typically a structure, which was discussed in the previous article.

How to Use the StructLayout Attribute to Control Memory Layout in C# and C++ Platform Invocation (P/Invoke)

Leave a Comment