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