M
Mike
Within the following structure, TopStruct, I'd like to create 3 other
structures, 2 of which make up a union. The first structure will always
contain some data that I need and should never be overwritten, i.e. should
not be part of the union. The remaining 2, UnionData1 and UnionData2, make
up the union and are populated as needed.
I want to be certain that this code will work as planned. Specifically, if
the field offsets of the first union members are set to '0', will they
overwrite the data in the first structure, AlwaysContainsData? Or should
they be changed to the values indicated in the inline comments?
Essentially, I don't know if the FieldOffset attribute applies to the struct
in which it's called, e.g. UnionData1, or to the top-level struct,
TopStruct:
public struct TopStruct
{
[StructLayout( LayoutKind.Sequential )]
public struct AlwaysContainsData // Contains 262 bytes of data.
{
public short dataType;
public short rmsVolt;
public short rmsCurr;
[MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 )]
public short[] waveform;
}
// Begin the structs for the union.
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData1
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;
}
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData2
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 128 ) ] // or should it be "FieldOffset( 390 )"
?
public short[] waveform3;
}
} // End of TopStruct
structures, 2 of which make up a union. The first structure will always
contain some data that I need and should never be overwritten, i.e. should
not be part of the union. The remaining 2, UnionData1 and UnionData2, make
up the union and are populated as needed.
I want to be certain that this code will work as planned. Specifically, if
the field offsets of the first union members are set to '0', will they
overwrite the data in the first structure, AlwaysContainsData? Or should
they be changed to the values indicated in the inline comments?
Essentially, I don't know if the FieldOffset attribute applies to the struct
in which it's called, e.g. UnionData1, or to the top-level struct,
TopStruct:
public struct TopStruct
{
[StructLayout( LayoutKind.Sequential )]
public struct AlwaysContainsData // Contains 262 bytes of data.
{
public short dataType;
public short rmsVolt;
public short rmsCurr;
[MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 )]
public short[] waveform;
}
// Begin the structs for the union.
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData1
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;
}
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData2
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 128 ) ] // or should it be "FieldOffset( 390 )"
?
public short[] waveform3;
}
} // End of TopStruct