/// <summary> /// 把 list 送入非托管空间,返回其指针 /// </summary> /// <param name="list"></param> /// <returns></returns> public static IntPtr slist2_ptr(SPair[] list) { if (list == null || list.Length == 0) { return(IntPtr.Zero); } int sz = Marshal.SizeOf(typeof(NativePcsSList2)); IntPtr lstPtr = Marshal.AllocHGlobal(sz * list.Length); IntPtr p = lstPtr; byte[] bytes; NativePcsSList2 item; for (int i = 0; i < list.Length; i++) { item = new NativePcsSList2(); item.str1 = utf8_str_ptr(list[i].str1); item.str2 = utf8_str_ptr(list[i].str2); if (i < list.Length - 1) /*不是最后一个*/ { item.next = IntPtrAdd(p, Marshal.SizeOf(typeof(NativePcsSList2))); } else { item.next = IntPtr.Zero; } bytes = StructToBytes(item); Marshal.Copy(bytes, 0, p, bytes.Length); p = item.next; } return(lstPtr); }
/// <summary> /// 释放非托管空间的由 slist2_ptr() 创建的空间 /// </summary> /// <param name="ptr"></param> public static void free_slist2_ptr(IntPtr ptr) { IntPtr p = ptr; while (p != IntPtr.Zero) { NativePcsSList2 item = (NativePcsSList2)Marshal.PtrToStructure(p, typeof(NativePcsSList2)); if (item.str1 != IntPtr.Zero) { free_str_ptr(item.str1); } if (item.str2 != IntPtr.Zero) { free_str_ptr(item.str2); } p = item.next; } Marshal.FreeHGlobal(ptr); }
/// <summary> /// 把 list 送入非托管空间,返回其指针 /// </summary> /// <param name="list"></param> /// <returns></returns> public static IntPtr slist2_ptr(SPair[] list) { if (list == null || list.Length == 0) return IntPtr.Zero; int sz = Marshal.SizeOf(typeof(NativePcsSList2)); IntPtr lstPtr = Marshal.AllocHGlobal(sz * list.Length); IntPtr p = lstPtr; byte[] bytes; NativePcsSList2 item; for (int i = 0; i < list.Length; i++) { item = new NativePcsSList2(); item.str1 = utf8_str_ptr(list[i].str1); item.str2 = utf8_str_ptr(list[i].str2); if (i < list.Length - 1) /*不是最后一个*/ item.next = IntPtrAdd(p, Marshal.SizeOf(typeof(NativePcsSList2))); else item.next = IntPtr.Zero; bytes = StructToBytes(item); Marshal.Copy(bytes, 0, p, bytes.Length); p = item.next; } return lstPtr; }