Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 Pack Field
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
StructLayoutAttribute..::.Pack Field

Updated: July 2009

Controls the alignment of data fields of a class or structure in memory.

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Pack As Integer
Visual Basic (Usage)
Dim instance As StructLayoutAttribute
Dim value As Integer

value = instance.Pack

instance.Pack = value
C#
public int Pack
Visual C++
public:
int Pack
JScript
public var Pack : int

This field indicates memory boundaries that should be used when the LayoutKind..::.Sequential value is specified.

The value of Pack must be 0, 1, 2, 4, 8, 16, 32, 64, or 128. A value of 0 indicates that the packing alignment is set to the default for the current platform. A value of 1 indicates that data alignment occurs on byte boundaries. There are no gaps between fields with a packing value of 1.

Packing values of 2 and higher will cause each field to be aligned on a byte offset relative to the beginning of the structure. As such, data fields will start on offsets that are multiples of the requested packing value.

The following code sample contains a structure with three one-byte fields:

struct MyStruct
{
byte B0;
byte B1;
byte B2;
}

Byte B0 always starts at offset 0 (byte 0). A packing value of 0 will cause byte B1 to start at offset 0 (byte 0). Byte B1 will begin at offset 1 (byte 1), and byte B2 will begin at offset 2 (byte 2), because the default platform alignment will always contiguously pack identical types.

A packing value of 1 will produce the following:

B0 will still begin at offset 0 (byte 0).

B1 will still begin at offset 1 (byte 1).

B2 will still begin at offset 2 (byte 2).

However, a packing value of 2 will produce the following:

B0 will still begin at offset 0 (byte 0).

B1 will begin at offset 2 (byte 2).

B3 will begin at offset 4 (byte 4).

The Pack attribute is frequently used when structures are exported during disk and network writes. The attribute is also frequently used during p/invoke and interop operations.

Occasionally the attribute is used to produce a tighter packing size, and thus reduce memory requirements. This type of usage requires careful consideration of actual hardware constraints, and may actually degrade performance.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

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

Date

History

Reason

July 2009

Improved remarks.

Information enhancement.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
DDK Structure Alignment      Garth Bushell   |   Edit   |   Show History
It's be really nice if there was a Attribute called DDKStructPack To make .NET code easier to on 32 and 64 bit platforms.
as I find myself doing this alot when talking to the DDK from .NET.

// Hmm. the DDK has packing at 1 for 32bit platforms and 8 for 64bit..
// How do I do this nicely?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA_W_32
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string DevicePath;
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA_W_64
{
public int cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string DevicePath;
}


Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker