Export (0) Print
Expand All
Expand Minimize

LayoutKind Enumeration

Controls the layout of an object when exported to unmanaged code.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum LayoutKind
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum LayoutKind
SerializableAttribute 
ComVisibleAttribute(true) 
public enum LayoutKind

 Member nameDescription
Supported by the .NET Compact FrameworkAutoThe runtime automatically chooses an appropriate layout for the members of an object in unmanaged memory. Objects defined with this enumeration member cannot be exposed outside of managed code. Attempting to do so generates an exception. 
Supported by the .NET Compact FrameworkExplicitThe precise position of each member of an object in unmanaged memory is explicitly controlled. Each member must use the FieldOffsetAttribute to indicate the position of that field within the type. 
Supported by the .NET Compact FrameworkSequentialThe members of the object are laid out sequentially, in the order in which they appear when exported to unmanaged memory. The members are laid out according to the packing specified in StructLayoutAttribute.Pack, and can be noncontiguous. 

This enumeration is used with StructLayoutAttribute. The common language runtime uses the Auto layout value by default. To reduce layout-related problems associated with the Auto value, C#, Visual Basic, and C++ compilers specify Sequential layout for value types.

The following example shows the managed declaration of the PtInRect function, which checks whether a point lies within a rectangle, and defines a Point structure with Sequential layout and a Rect structure with Explicit layout.

enum Bool
{
   False = 0,
   True
};
[StructLayout(LayoutKind.Sequential)]
public struct Point 
{
   public int x;
   public int y;
}   

[StructLayout(LayoutKind.Explicit)]
public struct Rect 
{
   [FieldOffset(0)] public int left;
   [FieldOffset(4)] public int top;
   [FieldOffset(8)] public int right;
   [FieldOffset(12)] public int bottom;
}   

class LibWrapper
{
   [DllImport("user32.dll", CallingConvention=CallingConvention.StdCall)]
   public static extern Bool PtInRect(ref Rect r, Point p);
};

class TestApplication
{
   public static void Main()
   {
      try
      {
         Bool bPointInRect = 0;
         Rect myRect = new Rect();
         myRect.left = 10;
         myRect.right = 100;
         myRect.top = 10;
         myRect.bottom = 100;
         Point myPoint = new Point();
         myPoint.x = 50;
         myPoint.y = 50;
         bPointInRect = LibWrapper.PtInRect(ref myRect, myPoint);
         if(bPointInRect == Bool.True)
            Console.WriteLine("Point lies within the Rect");
         else
            Console.WriteLine("Point did not lies within the Rect");
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}

enum Bool
{
    False (0),
    True (1);
} //Bool

/** @attribute StructLayout(LayoutKind.Sequential)
 */
public class Point
{
    public static int x;
    public static int y;
} //Point

/** @attribute StructLayout(LayoutKind.Explicit)
 */
public class Rect
{
    /** @attribute FieldOffset(0) */ public int left;

    /** @attribute FieldOffset(4) */ public int top;

    /** @attribute FieldOffset(8) */ public int right;

    /** @attribute FieldOffset(12) */ public int bottom;
} //Rect

class LibWrapper
{
    /** @attribute DllImport("user32.dll", 
        CallingConvention = CallingConvention.StdCall)
     */
    public static native Bool PtInRect(/** @ref */ Rect r, Point p);
} //LibWrapper

class TestApplication
{
    public static void main(String[] args)
    {
        try {
            Bool bPointInRect = (Bool)0;
            Rect myRect = new Rect();
            myRect.left = 10;
            myRect.right = 100;
            myRect.top = 10;
            myRect.bottom = 100;
            Point myPoint = new Point();
            myPoint.x = 50;
            myPoint.y = 50;
            bPointInRect = LibWrapper.PtInRect(/** @ ref */ myRect, myPoint);
            if (bPointInRect.Equals(Bool.True)) {
                Console.WriteLine("Point lies within the Rect");
            }
            else {
                Console.WriteLine("Point does not lie within the Rect");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.get_Message());
        }
    } //main
} //TestApplication

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft