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 属性。

解决方案 »

  1.   

    那这个东西应该写在什么地方??[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public struct STRINGBUFFER
    {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
      public string szText;
    }是不是应该这么用》?
      

  2.   

    //Wingdi.h, gdi32.dlltypedef struct tagPoint{
    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);