Export (0) Print
Expand All

StructLayoutAttribute Class

The StructLayoutAttribute class allows the user to control the physical layout of the data fields of a class or structure.

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

[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false)] 
public sealed class StructLayoutAttribute : Attribute
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false) */ 
public final class StructLayoutAttribute extends Attribute
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false) 
public final class StructLayoutAttribute extends Attribute

You can apply this attribute to classes or structures.

Typically, the common language runtime controls the physical layout of the data fields of a class or structure in managed memory. If the class or structure needs to be arranged a certain way, you can use StructLayoutAttribute. Explicit control of a class layout is important if the class is to be passed to unmanaged code that expects a specific layout. The LayoutKind value Sequential is used to force the members to be laid out sequentially in the order they appear. Explicit controls the precise position of each data member. With Explicit, each member must use the FieldOffsetAttribute to indicate the position of that field within the type.

C#, Visual Basic. NET, and C++ compilers apply the Sequential layout value to structures by default. For classes, you must apply the Sequential value explicitly. The Type Library Importer (Tlbimp.exe) also applies this attribute; it always applies the Sequential value when it imports a type library.

The following example demonstrates a managed declaration of the GetSystemTime function and defines MySystemTime class with LayoutKind.Explicit layout. The GetSystemTime gets the system time and prints to the console.

[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
public class MySystemTime 
{
   [FieldOffset(0)]public ushort wYear; 
   [FieldOffset(2)]public ushort wMonth;
   [FieldOffset(4)]public ushort wDayOfWeek; 
   [FieldOffset(6)]public ushort wDay; 
   [FieldOffset(8)]public ushort wHour; 
   [FieldOffset(10)]public ushort wMinute; 
   [FieldOffset(12)]public ushort wSecond; 
   [FieldOffset(14)]public ushort wMilliseconds; 
}

class LibWrapper
{
   [DllImport("kernel32.dll")]
   public static extern void GetSystemTime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st);
};

class TestApplication
{      
   public static void Main()
   {
      try
      {
         MySystemTime sysTime = new MySystemTime();
         LibWrapper.GetSystemTime(sysTime);
         Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay,
            sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);            
      }         
      catch(TypeLoadException e)
      {
         Console.WriteLine("TypeLoadException : " + e.Message);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}

/** @attribute StructLayout(LayoutKind.Explicit, Size = 16, 
    CharSet = CharSet.Ansi)
 */
public class MySystemTime
{
    /** @attribute FieldOffset(0)
     */
    public short wYear;
    
    /** @attribute FieldOffset(2)
     */
    public short wMonth;
    
    /** @attribute FieldOffset(4)
     */
    public short wDayOfWeek;

    /** @attribute FieldOffset(6)
     */
    public short wDay;

    /** @attribute FieldOffset(8)
     */
    public short wHour;

    /** @attribute FieldOffset(10)
     */
    public short wMinute;

    /** @attribute FieldOffset(12)
     */
    public short wSecond;

    /** @attribute FieldOffset(14)
     */
    public short wMilliseconds;

} //MySystemTime

class LibWrapper
{
    /** @attribute DllImport("kernel32.dll")
     */
    public static native void GetSystemTime(
        /** @attribute MarshalAs(UnmanagedType.LPStruct)
         */
        MySystemTime st);
} //LibWrapper

class TestApplication
{
    public static void main(String[] args)
    {
        try {
            MySystemTime sysTime = new MySystemTime();
            LibWrapper.GetSystemTime(sysTime);
            Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}",
                new Object[] { (Int32)sysTime.wDay, (Int32)sysTime.wMonth,
                (Int32)sysTime.wYear, (Int32)sysTime.wHour, (Int32)sysTime.
                wMinute, (Int32)sysTime.wSecond });
        }
        catch (TypeLoadException e) {
            Console.WriteLine("TypeLoadException : " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.get_Message());
        }
    } //main
} //TestApplication

System.Object
   System.Attribute
    System.Runtime.InteropServices.StructLayoutAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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