justinstenning / SharedMemory

C# shared memory classes for sharing data between processes (Array, Buffer and Circular Buffer)
https://www.nuget.org/packages/SharedMemory
Other
566 stars 118 forks source link

FastStructure is incompatible with some managed MarshalAs scenarios #9

Closed justinstenning closed 8 years ago

justinstenning commented 8 years ago

The following field will not work correctly in FastStructure:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8, ArraySubType = UnmanagedType.U1)]
public byte[] Contents;

Instead it needs to be in a LayoutKind.Explicit struct with a fixed byte array, e.g.

[FieldOffset(48)]
public fixed byte Contents[8];

However this will then not work correctly with Marshal.PtrToStructure either. Instead only the first byte will be copied into the structure.

Need to either implement checks in FastStructure static constructor so that users of the library are notified of issues ASAP, or find a work around.

justinstenning commented 8 years ago

Testing shows that it can be in a LayoutKind.Sequential without the FieldOffset

justinstenning commented 8 years ago

Implemented