Looking at the two structures, they obviously differ in that the unmanaged starts with a float
, and the managed starts with a double
. Obviously that means that the structs do not match. The managed struct is 4 bytes longer, and all members after the first are offset by 4 bytes.
Now, you say that when you have the code as per the question, the first field contains garbage, but the other fields are fine. From which the only reasonable conclusion is that the write side of the interface is writing to an address offset by 4 bytes from that which the read side is reading.
Alignment and packing are not the issue here. You are reading from an address 4 bytes different from that which is used to perform the write.
The solution is to:
- Make sure that you write to and read from the same location in shared memory.
- Define your structs to be identical.