public static void Call_Get() { var list = new List <MyIntroStruct>(); //call SetMyIntroStruct var size = Marshal.SizeOf(typeof(MyIntroStruct)); IntPtr ptr = IntPtr.Zero; //分配大小 int count = 1; GetMyIntroStruct(ptr, ref count); if (count > 0) { ptr = Marshal.AllocHGlobal(count * size); } if (ptr != IntPtr.Zero) { GetMyIntroStruct(ptr, ref count); } for (int i = 0; i < count; i++) { IntPtr p = IntPtr.Add(ptr, i * size); MyIntroStruct mm = (MyIntroStruct)Marshal.PtrToStructure(p, typeof(MyIntroStruct)); list.Add(mm); } Marshal.FreeHGlobal(ptr); var res = new List <MyIntroStruct2>(); list.ForEach(s => { MyIntroStruct2 s2; Convert(s, out s2); res.Add(s2); }); }
public static int Convert(MyIntroStruct s1, out MyIntroStruct2 s2) { s2 = new MyIntroStruct2(); s2.BB = s1.B; s2.C = s1.C; s2.CharsA = Utf8ToString(s1.CharsA); s2.CharsB = Utf8ToString(s1.CharsB); return(0); }
public static void Call_Set() { IntPtr ptr = IntPtr.Zero; ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MyIntroStruct))); MyIntroStruct ms = new MyIntroStruct(); ms.B = false; ms.C = 998; ms.CharsA = StringToUTF8Inptr("123456789"); ms.CharsB = IntPtr.Zero; //ms.CharsB = StringToUTF8Inptr("KKKK"); Marshal.StructureToPtr(ms, ptr, true); SetMyIntroStruct(ptr, 9); Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ms.CharsA); Marshal.FreeHGlobal(ms.CharsB); }