StructLayout 属性用于指定结构的字段的布局。namespace System.Runtime.InteropServices
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class StructLayoutAttribute: System.Attribute
{
public StructLayoutAttribute(LayoutKind value) {...}
public CharSet CharSet;
public bool CheckFastMarshal;
public int Pack;
public LayoutKind Value { get {...} }
}
}
如果指定了 LayoutKind.Explicit,则结构中的每个字段都必须具有 FieldOffset 属性。如果未指定 LayoutKind.Explicit,则禁止使用 FieldOffset 属性。
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class StructLayoutAttribute: System.Attribute
{
public StructLayoutAttribute(LayoutKind value) {...}
public CharSet CharSet;
public bool CheckFastMarshal;
public int Pack;
public LayoutKind Value { get {...} }
}
}
如果指定了 LayoutKind.Explicit,则结构中的每个字段都必须具有 FieldOffset 属性。如果未指定 LayoutKind.Explicit,则禁止使用 FieldOffset 属性。
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public string szText;
}是不是应该这么用》?
LONG x;
LONG y;
} POINT, * LPPOINT;MoveToEx(HDC hdc, int x, int y, LPPOINT oldPoint);//C#中使用MoveToEx
[StructLayout(LayoutKind.Sequential)]//确保字段顺序和上面的POINT结构对应。
public struct POINT
{
int x;
int y;
};public static extern bool MoveToEx(System.IntPtr hdc, int x, int y, ref POINT oldPoint);