0

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.

nagates
  • 620
  • 13
  • 40
  • You did not declare a variable-sized struct. You can't. This question has been asked many times before, review the existing ones before you ask your own. Put "[pinvoke] variable sized struct" in the search box at the upper right of this page. – Hans Passant Jul 07 '14 at 16:59
  • [Marshall.Sizeof](http://msdn.microsoft.com/en-us/library/vstudio/y3ybkfb3%28v=vs.100%29.aspx) returns the size of any instance of a specific type, not that memory size plus the size of any memory held onto in (managed or unmanaged) references. It's the equivalent of the old-time [C sizeof](http://msdn.microsoft.com/en-us/library/0w557fh7.aspx) operator. – dbc Jul 07 '14 at 17:00
  • The question doesn't make much sense to me. The size of a pointer is independent from the size of the buffer it points to. In any case we can't really help you with the marhsalling until we know what's on the unmanaged side. – David Heffernan Jul 07 '14 at 18:19
  • The answers at that suggested dupe. Isn't there a dupe somewhere with good answers? – David Heffernan Jul 07 '14 at 20:10
  • Yes, I guess rather than Marshaling a pointer to my data, I need to marshal a variable width array, so I can't set the SizeConst, because I wont know until run time. – nagates Jul 07 '14 at 20:48
  • Please can you show the unmanaged side of the interop. Are you in control of both sides of the interop? If you can control both sides you can probably find an easier solution. – David Heffernan Jul 07 '14 at 21:01
  • @DavidHeffernan, Possibly, this is talking to a driver, but yes, I will see what I can do. Thanks. – nagates Jul 07 '14 at 21:14
  • Your first step is to understand how the memory is laid out in the native code, and why your `IntPtr Frame` does not match. – David Heffernan Jul 08 '14 at 05:56

0 Answers0