Problems with creating Structures in .NET

When I try to create the following structure in VB.NET...


        <StructLayout(LayoutKind.Sequential)> Structure Struct1
            <FieldOffset(0)> Public Unk1 As Short
            <FieldOffset(35), MarshalAs(UnmanagedType.ByValArray, SizeConst:=23)> Public unk2() As Char
            <FieldOffset(97), MarshalAs(UnmanagedType.ByValArray, SizeConst:=23)> Public unk3() As Char
        End Structure

 

I get the following error...

An error occurred creating the form. See Exception.InnerException for details.  The error is: Could not load type 'Struct1' from assembly 'StructTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 35 that is incorrectly aligned or overlapped by a non-object field.

Any help would be appreciated-- thanks!

-Chad




Answer this question

Problems with creating Structures in .NET

  • aahmad

    Are you sure that you are using sequential layout You cannot specify sequential layout and also specify field offsets. If you look at the docs, you'll see that the two together don't make conceptual sense.

    I cannot repro your your issue. Can you send me an actual .vb file that has the problem

    thanks -- rich


  • farukh

    You might also like to take a look at the feedback that I gave another developer @ http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx feedbackid=a35b402b-00ce-43da-8e63-c16f834b8805. It is potentially very related to your scenario.

    thanks -- rich


  • lx1973

    A Char is 16 bits (2 bytes) so I would assume it needs to be aligned on a n even boundary.
  • Kamal_mahendra

    Well even if I do comment one of them out I still get the Error.  I can't figure out for the life of me why I'm getting this error.

    EDIT: And oh, I tried As Byte also and still received this error.

    -Chad



  • Problems with creating Structures in .NET