none
Exception: Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout

    Question

  • Hello Experts,

    I am facing a strange problem.(its strange to me as i am not so proficient in C#)
    Following are my structure declarations:

        public struct  ElementId
        {
            public byte u8SystemID;

            public byte u8MachineID;

            public short u16AppID;

            public uint u32DriverID;

            public uint u32DeviceID;
        };

        public enum Status
        {
            online = 1,
            offline = 2
        };
        public struct  RepositoryData
        {
            public  ElementId ID;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] u8Key;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public byte[] u8Name;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] u8Type;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] u8MachineIP;

            public  Status Status;
        };
       
    I am trying to convert RepositoryData to IntPtr using StructureToPtr function.
    This code for that conversion is below:
    RepositoryData data;
    IntPtr repDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(data));

    Marshal.StructureToPtr(data, repDataPtr, false);

    When C# executes "Marshal.StructureToPtr(data, repDataPtr, false);" line it gives following exception:

    Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

    I am not sure how can I resolve this error. I want to convert a structure to IntPtr which will be passed to C++ DLL further. Please guide me how can I do this.

    Thanks in advance.

    Wednesday, June 03, 2009 2:19 PM

Answers

  • Here's an example of a program that generates that exception:

    using System;
    using System.Runtime.InteropServices;

    class Program {
        static void Main(string[] args) {
            RepositoryData test = new RepositoryData();
            test.u8Key = new byte[63];   // <== kaboom here
            int len = Marshal.SizeOf(test);
            IntPtr ptr = Marshal.AllocHGlobal(len);
            Marshal.StructureToPtr(test, ptr, false);
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RepositoryData {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] u8Key;
        }
    }

    In other words, it cannot convert an array with 63 bytes to 64 bytes in the marshaled data.  Check your "data" initialization.

    Hans Passant.
    • Marked as answer by rinks24583 Thursday, June 04, 2009 5:49 AM
    Wednesday, June 03, 2009 5:04 PM
    Moderator

All replies

  • Here's an example of a program that generates that exception:

    using System;
    using System.Runtime.InteropServices;

    class Program {
        static void Main(string[] args) {
            RepositoryData test = new RepositoryData();
            test.u8Key = new byte[63];   // <== kaboom here
            int len = Marshal.SizeOf(test);
            IntPtr ptr = Marshal.AllocHGlobal(len);
            Marshal.StructureToPtr(test, ptr, false);
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RepositoryData {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] u8Key;
        }
    }

    In other words, it cannot convert an array with 63 bytes to 64 bytes in the marshaled data.  Check your "data" initialization.

    Hans Passant.
    • Marked as answer by rinks24583 Thursday, June 04, 2009 5:49 AM
    Wednesday, June 03, 2009 5:04 PM
    Moderator
  • Thanks Man.

    It worked for me and my problem is resolved.

    Thursday, June 04, 2009 5:49 AM