词条 | 边界对齐 |
释义 | 边界对齐是指编辑C++时在使用结构体指针,进行C#和C++的互相调用。边界对齐是一个大问题,因为边界对齐问题,结构体的成员并不是顺序在内存一个挨着一个的排序。 而且在C++中可以使用#pragma pack(n)改变边界对齐的方案,那C#的结构体怎么对应C++的结构体那? 第一:最普通的情况下,C++代码没有使用#pragma pack(n)改变边界对齐,这里C#可以使用两种方法处理,LayoutKind.Explicit 和 LayoutKind.Sequential,建议使用后者,虽然前者是万金油,不过使用起来太累又爱出错。 C++: 1 struct Test1 2 { 3 int test1; 4 char test2; 5 __int64 test3; 6 short test4; 7 }; 8 9 Test1 * __stdcall GetTest1() 10 { 11 test1.test1 = 10; 12 test1.test2 = 11; 13 test1.test3 = 12; 14 test1.test4 = 13; 15 return &test1; 16 } C#:(这里有两种方案,使用LayoutKind.Explicit 和LayoutKind.Sequential,注意一下) 1 [StructLayout(LayoutKind.Explicit)] 2 public struct Test 3 { 4 [FieldOffset(0)] 5 public int test1; 6 [FieldOffset(4)] 7 public char test2; 8 [FieldOffset(8)] 9 public Int64 test3; 10 [FieldOffset(16)] 11 public short test4; 12 } 13 14 [StructLayout(LayoutKind.Sequential)] 15 public struct Test1 16 { 17 public int test1; 18 public char test2; 19 public Int64 test3; 20 public short test4; 21 } 22 23 [DllImport("TestDll")] 24 public static extern IntPtr GetTest1(); 25 26 //################################# 27 IntPtr p = GetTest1(); 28 Test test = (Test)Marshal.PtrToStructure(p, typeof(Test)); 29 Console.WriteLine(test.test1 + test.test2 + test.test3 + test.test4); 30 31 IntPtr p1 = GetTest1(); //Auto pack 32 Test1 test1 = (Test1)Marshal.PtrToStructure(p1, typeof(Test1)); 33 Console.WriteLine(test1.test1 + test1.test2 + test1.test3 + test1.test4) |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。