.Net StructLayoutKind.Explicit and structure packing

Status
Not open for further replies.
I've got a native structure with a union that I'm trying to implement in .Net. I know I have to use field offsets and such and I thought I had it right, but the problem I ran into is as soon as I tried to declare an instance of the structure I got an exception that field offsets straddled alignment boundaries. This happened regardless of whether I explicitly set the structure pack attribute to Pack:=1 or not.

Is this a bug, intended behavior or is there something I'm missing here?
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
Is one of the fields in question a reference type?

They must be aligned (to native pointer size, so basically 8 byte boundaries if you want it to work generally) but also they cannot overlap with any non reference types either so it is quite possible that the structure you use in unmanaged code simply won't work in managed world.

The GC process has to be able to traverse the live object graph and if you had a string reference aliased with a long how would it know which is which?
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
Oh and I should say that explicit layout structs amount to much the same as unsafe code in the sense that you really should know what you're doing before using them in anger. But you're asking so you're doing the right thing.

You should remember that, the size of a strict is not guaranteed unless you apply the Size property, calling this() from your constructors helpfully deals with the requirement for explicit initialisation of all fields, simply replacing a reference field with IntPtr won't work unless you pin the referenced object, make it immutable unless you *really* know what you're doing.
 
Thing is the exact same structure works fine if LayoutKind.Sequential is used. For example (pardon the VB):

Code:
<StructLayout(LayoutKind.Explicit, Pack:=1, CharSet:=CharSet.Ansi)> _
Public Structure SOMESTRUCT
   <FieldOffset(0), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10) Public somestring As String
   <FieldOffset(10), MarshalAs(UnmanagedType.ByValArray, SizeConst:=10) Public somearray() As Integer
   ...
End Structure

Throws the mentioned exception. However, if you change it to LayoutKind.Sequential, it's more than happy. Hence... my confusion.

Anyway, it appears that I've sort of worked around this issue by making somearray() into its own structure (it's actually a union on the C-side with another member) defined as LayoutKind.Explicit, since it's at the beginning of the structure alignment doesn't matter much. And SOMESTRUCT is set as LayoutKind.Sequential with Pack:=1 and the second member as a type of the structure that hosts somearray(). It's not pretty, but it seems to work. I'm just trying to make sure the marshaler is happy with what's going on here.

To be fair, the .Net side of things really isn't MY problem, I'm just trying to help someone out, as well as make sure that the interop with my native library works.
 
Status
Not open for further replies.