Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
LayoutKind Enumeration
Collapse the table of content
Expand the table of content

LayoutKind Enumeration

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

[Visual Basic]
<Serializable>
Public Enum LayoutKind
[C#]
[Serializable]
public enum LayoutKind
[C++]
[Serializable]
__value public enum LayoutKind
[JScript]
public
   Serializable
enum LayoutKind

Remarks

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 .NET, and C++ compilers specify Sequential layout for value types.

Members

Member name Description
Auto

Supported by the .NET Compact Framework.

The 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.
Explicit

Supported by the .NET Compact Framework.

The 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.
Sequential

Supported by the .NET Compact Framework.

The 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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Enum Bool
   [False] = 0
   [True]
End Enum 'Bool
<StructLayout(LayoutKind.Sequential)>  _
Public Structure Point
   Public x As Integer
   Public y As Integer
End Structure 'Point

<StructLayout(LayoutKind.Explicit)>  _   
Public Structure Rect
   <FieldOffset(0)> Public left As Integer
   <FieldOffset(4)> Public top As Integer
   <FieldOffset(8)> Public right As Integer
   <FieldOffset(12)> Public bottom As Integer
End Structure 'Rect


Class LibWrapper
   
   <DllImport("user32.dll", CallingConvention := CallingConvention.StdCall)>  _
   Public Shared Function PtInRect(ByRef r As Rect, p As Point) As Bool
   End Function    
End Class 'LibWrapper


Class TestApplication
   
   Public Shared Sub Main()
      Try
         Dim bPointInRect As Bool = 0
         Dim myRect As New Rect()
         myRect.left = 10
         myRect.right = 100
         myRect.top = 10
         myRect.bottom = 100
         Dim myPoint As New Point()
         myPoint.x = 50
         myPoint.y = 50
         bPointInRect = LibWrapper.PtInRect(myRect, myPoint)
         If bPointInRect = Bool.True Then
            Console.WriteLine("Point lies within the Rect")
         Else
            Console.WriteLine("Point did not lies within the Rect")
         End If
      Catch e As Exception
         Console.WriteLine(("Exception : " + e.Message.ToString()))
      End Try
   End Sub 'Main
End Class 'TestApplication

[C#] 
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);
      }
   }
}

[C++] 
__value enum Bool {
    False = 0,
    True
};

[StructLayout(LayoutKind::Sequential)]
__value struct Point {
public:
    int  x;
public:
    int  y;
};

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

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

    int main() {
        try {
            Bool bPointInRect = (Bool)0;
            Rect myRect = Rect();
            myRect.left = 10;
            myRect.right = 100;
            myRect.top = 10;
            myRect.bottom = 100;
            Point myPoint = Point();
            myPoint.x = 50;
            myPoint.y = 50;
            bPointInRect = LibWrapper::PtInRect(&myRect, myPoint);
            if (bPointInRect == Bool::True)
                Console::WriteLine(S"Point lies within the Rect");
            else
                Console::WriteLine(S"Point did not lie within the Rect");
        } catch (Exception* e) {
            Console::WriteLine(S"Exception : {0}", e->Message);
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Runtime.InteropServices Namespace

Show:
© 2015 Microsoft