Expand
StructLayoutAttribute Class

Lets you control the physical layout of the data fields of a class or structure.

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

'Declaration

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct, Inherited := False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class StructLayoutAttribute _
	Inherits Attribute
Remarks

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. However, if you want to arrange the class or structure needs in 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, and C++ compilers apply the Sequential layout value to structures by default. For classes, you must apply the Sequential value explicitly. The Tlbimp.exe (Type Library Importer) also applies this attribute; it always applies the Sequential value when it imports a type library.

Examples

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.


Imports System
Imports System.Runtime.InteropServices

Namespace InteropSample

   <StructLayout(LayoutKind.Explicit, Size:=16, CharSet:=CharSet.Ansi)> _
   Public Class MySystemTime
      <FieldOffset(0)> Public wYear As Short
      <FieldOffset(2)> Public wMonth As Short
      <FieldOffset(4)> Public wDayOfWeek As Short
      <FieldOffset(6)> Public wDay As Short
      <FieldOffset(8)> Public wHour As Short
      <FieldOffset(10)> Public wMinute As Short
      <FieldOffset(12)> Public wSecond As Short
      <FieldOffset(14)> Public wMilliseconds As Short
   End Class 'MySystemTime


   Class LibWrapper

      <DllImport("kernel32.dll")> _
      Public Shared Sub GetSystemTime(<MarshalAs(UnmanagedType.LPStruct)> ByVal st As MySystemTime)
      End Sub
   End Class 'LibWrapper

   Class TestApplication

      Public Shared Sub Main()
         Try
            Dim sysTime As 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 e As TypeLoadException
            Console.WriteLine(("TypeLoadException : " + e.Message.ToString()))
         Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message.ToString()))
         End Try
      End Sub 'Main
   End Class 'TestApplication
End Namespace 'InteropSample 


Inheritance Hierarchy

System.Object
  System.Attribute
    System.Runtime.InteropServices.StructLayoutAttribute
Thread Safety

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Community ContentAdd
Page view tracker