I have a scenerio where I need to Marshal a struct that has a variable sized byte array.
I've used this question as my guide, but I am still having issue with the size.
Marshalling dynamic size array into struct
c++ Code
// frame headers
typedef struct _VENUS_HEADER
{
BOOLEAN Relative; // TRUE = relative timing (use Milliseconds)
// FALSE = absolute timing (use Tics)
VENUS_SEQUENCE Sequence; //sequence number
VENUS_TIMESTAMP Time; // relative time to display in Milliseconds
} VENUS_HEADER, *PVENUS_HEADER;
// WriteFile format
typedef struct _VENUS_IMAGE
{
VENUS_HEADER Header; // sequence & time
UCHAR Frame[1]; // frame data
} VENUS_IMAGE, *PVENUS_IMAGE;
C# Code
[StructLayout(LayoutKind.Sequential)]
public struct VENUS_TIMESTAMP
{
public long Tics;
}
[StructLayout(LayoutKind.Sequential)]
public struct VENUS_HEADER
{
[MarshalAs(UnmanagedType.Bool)]
public bool Relative; // TRUE = relative timing (use Milliseconds)
// FALSE = absolute timing (use Tics)
public uint Sequence; //sequence number
public VENUS_TIMESTAMP Time; // relative time to display in Milliseconds
}
// WriteFile format
[StructLayout(LayoutKind.Sequential)]
public struct VENUS_IMAGE
{
//public int MyProperty { get; set; }
public VENUS_HEADER Header; // sequence & time
public IntPtr Frame; // frame data
}
byte[] bulbData = bulb.Bitmap2Bulb(img);
venusImage.Frame = Marshal.AllocHGlobal(bulbData.Length); Marshal.Copy(bulbData, 0, venusImage.Frame, bulbData.Length);
int size = Marshal.SizeOf(venusImage);
My size reports as 24, regardless of the value of the IntPtr, or the byte[] size. I believe the final size of the struct should be 921616. Any hep would be appreciated.