Are P/Invoke [In, Out] attributes optional for marshaling arrays?

No, they are not exactly optional. It just happens to work by accident. It is however a very common accident. It works because the array doesn’t actually gets marshaled. The pinvoke marshaller sees that the C# array is already compatible with the native array so skips the step to create a copy of it. It merely pins the array and passes the pointer to the native code.

This is of course very efficient and you will inevitably get the results back because the native code is directly writing the array elements. So neither the [In] nor the [Out] attributes matter.

It gets much murkier if the array element type isn’t that simple. It isn’t that easy to identify an element type that’s a struct or class type that isn’t blittable or whose layout doesn’t match after marshaling so the pinvoke marshaller has to make a copy of the array. Especially the layout incompatibility can be very hard to identify because the managed layout is undiscoverable. And can change depending on the jitter that is used. It may work in x86 but not x64 for example, pretty nasty when AnyCPU is selected. Getting it to copy the modified copy back to the C# array does require [Out].

Not sure what to advice, other than that nobody ever got fired for being explicit in their declarations. Perhaps you should always be explicit when the array element type isn’t simple so you’ll never have an accident.

Leave a Comment