System.Runtime.InteropServices.LayoutKind Enumeration

Assembly: Mscorlib.dll
Namespace: System.Runtime.InteropServices
Summary
Controls the layout of an object when exported to unmanaged code.
C# Syntax:
[Serializable]
public enum LayoutKind
Remarks
This enumeration is used with StructLayoutAttribute.
Example
Illustrates a managed declaration of the PtInRect function which checks whether a point lies within a rectangle, and defines a Point structure with LayoutKind.Sequential layout and a Rect structure with LayoutKind.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);
         }
      }
   }

    
See also:
System.Runtime.InteropServices Namespace

System.Runtime.InteropServices.LayoutKind Member List:

Public Fields
Auto The runtime automatically chooses an appropriate layout for the members of an object in unmanaged memory.
Explicit 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 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.

Hierarchy:


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.