none
Marsheling Structures - LayoutKind.Auto RRS feed

  • Question

  • http://msdn2.microsoft.com/en-us/library/0t2cwe11(VS.71).aspx - Under LayoutKind.Automatic: it says "when a value type is passed to unmanaged code, the layout of the members is predictable. An attempt to marshal such a structure automatically causes an exception."

     

    According to link, value types cannot be marshaled when you set LayoutKind.Automatic.  Barring LayoutKind.Explicit, why must one maintain the same order in memory for a value type to be marsheled properly?

    Friday, April 4, 2008 7:37 PM

Answers

  • --why must one maintain the same order in memory for a value type to be marsheled properly

    This is because a structure in unmanaged application use a same kind of order while its managed counterpart will be automatically be arranged at runtime.

    For example, the sequence of two members in a structure in managed app might be re-ordered by the runtime, and this will not affect any managed application. But if you pass it to unmanaged application, bad things will happen.

    Thanks!
    Wednesday, April 9, 2008 5:28 AM

All replies

  • I have moved this thread to the .NET Development / Common Language Runtime forum for assistance with your questions.  Thanks.

    Monday, April 7, 2008 7:14 PM
    Moderator
  • --why must one maintain the same order in memory for a value type to be marsheled properly

    This is because a structure in unmanaged application use a same kind of order while its managed counterpart will be automatically be arranged at runtime.

    For example, the sequence of two members in a structure in managed app might be re-ordered by the runtime, and this will not affect any managed application. But if you pass it to unmanaged application, bad things will happen.

    Thanks!
    Wednesday, April 9, 2008 5:28 AM