System.Runtime.InteropServices.StructLayoutAttribute Class

public sealed class StructLayoutAttribute : Attribute

Base Types

Object
  Attribute
    StructLayoutAttribute

Assembly

mscorlib

Library

RuntimeInfrastructure

Summary

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

Description

The target objects for this attribute are classes and structures. By default, the physical layout of the data members of a target object is automatically arranged. When managed objects are passed as arguments to unmanaged code, the system creates their unmanaged representations. These unmanaged representations can be controlled with the StructLayoutAttribute . Such control is necessary if the unmanaged code expects a specific layout, packing size, or character set.

[Note: See the LayoutKind enumeration for a description of the possible layout schemes, and the FieldOffsetAttribute for further information on the layout of exported objects.]

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

Example

The following example demonstrates the use of the StructLayoutAttribute, and the FieldOffsetAttribute.

[Note: The non-standard PtInRect function used in this example indicates whether the specified point is located inside the specified rectangle. In this example, the layout setting on the Rect structure can be set to System.Runtime.InteropServices.LayoutKind.Sequential with no bearing on the end result.]

using System;
using System.Runtime.InteropServices;

[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 NativeCodeAPI {
 [DllImport("User32.dll")]
 public static extern bool PtInRect(ref Rect r, Point p);
}

public class StructLayoutTest {
 public static void Main() {
 Rect r;
 Point p1, p2;
 
 r.left = 0;
 r.right = 100;
 r.top = 0;
 r.bottom = 100;
 
 p1.x = 20;
 p1.y = 30;
 
 p2.x = 110;
 p2.y = 5;

 
 bool isInside1 = NativeCodeAPI.PtInRect(ref r, p1);
 bool isInside2 = NativeCodeAPI.PtInRect(ref r, p2);
 
 if(isInside1)
 Console.WriteLine("The first point is inside the rectangle.");
 else
 Console.WriteLine("The first point is outside the rectangle.");
 
 if(isInside2)
 Console.WriteLine("The second point is inside the rectangle.");
 else
 Console.WriteLine("The second point is outside the rectangle.");

 }
}
The output is

The first point is inside the rectangle.

The second point is outside the rectangle.

Attributes

AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple=false, Inherited=false)

See Also

System.Runtime.InteropServices Namespace

Members

StructLayoutAttribute Constructors

StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind) Constructor
StructLayoutAttribute(short) Constructor

StructLayoutAttribute Fields

StructLayoutAttribute.CharSet Field
StructLayoutAttribute.Pack Field
StructLayoutAttribute.Size Field

StructLayoutAttribute Properties

StructLayoutAttribute.Value Property


StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind) Constructor

public StructLayoutAttribute(LayoutKind layoutKind);

Summary

Constructs and initializes a new instance of the StructLayoutAttribute class with the specified LayoutKind value.

Parameters

layoutKind
A LayoutKind value that specifies how the class or structure is arranged in memory.

Description

If layoutKind contains an invalid LayoutKind value, a runtime error occurs.

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace

StructLayoutAttribute(short) Constructor

public StructLayoutAttribute(short layoutKind);

Summary

Constructs and initializes a new instance of the StructLayoutAttribute class with the specified value.

Parameters

layoutKind
A Int16 set to a LayoutKind value that specifies how the class or structure is arranged in memory.

Description

If the layoutKind parameter does not represent a valid LayoutKind value, a runtime error occurs.

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace

StructLayoutAttribute.CharSet Field

public CharSet CharSet;

Summary

A CharSet value that indicates the character set in which strings of an object are marshaled.

Description

[Note: See the CharSet enumeration for a description of different character sets.]

The default value of this field is System.Runtime.InteropServices.CharSet.Ansi.

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace

StructLayoutAttribute.Pack Field

public int Pack;

Summary

A Int32 that indicates the packing alignment used with the System.Runtime.InteropServices.LayoutKind.Sequential layout.

Description

The System.Runtime.InteropServices.StructLayoutAttribute.Pack field determines memory alignment of data fields of a target object.

Data fields of a target object exported to unmanaged memory are aligned on a byte boundary that is a multiple of System.Runtime.InteropServices.StructLayoutAttribute.Pack bytes, or at some natural alignment for that field type, whichever is smaller.

The value of System.Runtime.InteropServices.StructLayoutAttribute.Pack is required to be 0, 1, 2, 4, 8, 16, 32, 64, or 128. A value of zero indicates that the packing alignment is set to the default for the current platform. The default value is implementation-defined.

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace

StructLayoutAttribute.Size Field

public int Size;

Summary

A Int32 that indicates the size of the memory block to be allocated for an instance of the type qualified by the current StructLayoutAttribute.

Description

System.Runtime.InteropServices.StructLayoutAttribute.Size is required to be zero, or greater than or equal to the calculated size of the target object, based on the System.Runtime.InteropServices.StructLayoutAttribute.Pack field indicating the packing alignment. A System.Runtime.InteropServices.StructLayoutAttribute.Size of zero indicates that the size is calculated from the field types, their specified offsets, the packing size (default or specified) and natural alignment on the target, runtime platform.

[Note: For additional information on the System.Runtime.InteropServices.StructLayoutAttribute.Size field, see Partition II of the CLI Specification.]

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace

StructLayoutAttribute.Value Property

public LayoutKind Value { get; }

Summary

Gets the LayoutKind value that specifies how the target object is arranged.

Property Value

A LayoutKind value that specifies how the target object is arranged.

Description

This property is read-only.

See Also

System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace