StructLayoutAttribute Class
The StructLayoutAttribute class allows the user to control the physical layout of the data fields of a class or structure.
For a list of all members of this type, see StructLayoutAttribute Members.
System.Object
System.Attribute
System.Runtime.InteropServices.StructLayoutAttribute
[Visual Basic] <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct)> NotInheritable Public Class StructLayoutAttribute Inherits Attribute [C#] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public sealed class StructLayoutAttribute : Attribute [C++] [AttributeUsage(AttributeTargets::Class | AttributeTargets::Struct)] public __gc __sealed class StructLayoutAttribute : public Attribute [JScript] public AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct) class StructLayoutAttribute extends Attribute
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.
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. 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 classes and structures by default. The Type Library Importer (Tlbimp.exe) also applies this attribute; it always applies the Sequential value when it imports a type library.
Example
[Visual Basic, C#, C++] 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.
[Visual Basic] <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)> 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 [C#] [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); } } } [C++] [StructLayout(LayoutKind::Explicit, Size=16, CharSet=CharSet::Ansi)] __value class MySystemTime { public: [FieldOffset(0)] short int wYear; [FieldOffset(2)] short int wMonth; [FieldOffset(4)] short int wDayOfWeek; [FieldOffset(6)] short int wDay; [FieldOffset(8)] short int wHour; [FieldOffset(10)] short int wMinute; [FieldOffset(12)] short int wSecond; [FieldOffset(14)] short int wMilliseconds; }; __gc class LibWrapper { public: [DllImport(S"kernel32.dll")] static void GetSystemTime(MySystemTime* st); }; int main() { try { MySystemTime sysTime; LibWrapper::GetSystemTime(&sysTime); Console::WriteLine(S"The System time is {0}/{1}/{2} {3}:{4}:{5}", __box(sysTime.wDay), __box(sysTime.wMonth), __box(sysTime.wYear), __box(sysTime.wHour), sysTime.wMinute, sysTime.wSecond); } catch (TypeLoadException* e) { Console::WriteLine(S"TypeLoadException : {0}", e->Message); } 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
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
StructLayoutAttribute Members | System.Runtime.InteropServices Namespace | Type Library Importer (Tlbimp.exe)