1 namespace HandBrake.Interop
\r
4 using System.Collections.Generic;
\r
7 using System.Runtime.InteropServices;
\r
10 /// Helper utilities for native interop.
\r
12 public static class InteropUtilities
\r
15 /// Reads the given native structure pointer.
\r
17 /// <typeparam name="T">The type to convert the structure to.</typeparam>
\r
18 /// <param name="structPtr">The pointer to the native structure.</param>
\r
19 /// <returns>The converted structure.</returns>
\r
20 public static T ReadStructure<T>(IntPtr structPtr)
\r
22 return (T)Marshal.PtrToStructure(structPtr, typeof(T));
\r
26 /// Converts the given native HandBrake list to a managed list.
\r
28 /// <typeparam name="T">The type of structure in the list.</typeparam>
\r
29 /// <param name="listPtr">The pointer to the native list.</param>
\r
30 /// <returns>The converted managed list.</returns>
\r
31 public static List<T> ConvertList<T>(IntPtr listPtr)
\r
33 List<T> returnList = new List<T>();
\r
34 hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);
\r
36 for (int i = 0; i < itemList.items_count; i++)
\r
38 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));
\r
39 returnList.Add(ReadStructure<T>(itemPtr));
\r
46 /// Creates a native HandBrake list from the given managed list of pointers.
\r
48 /// <param name="list">The managed list to convert.</param>
\r
49 /// <returns>The converted native list.</returns>
\r
50 public static NativeList CreateIntPtrList(List<IntPtr> list)
\r
52 NativeList returnList = new NativeList();
\r
53 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
55 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
56 returnList.AllocatedMemory.Add(nativeListInternal);
\r
57 for (int i = 0; i < list.Count; i++)
\r
59 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);
\r
62 hb_list_s nativeListStruct = new hb_list_s();
\r
63 nativeListStruct.items = nativeListInternal;
\r
64 nativeListStruct.items_alloc = list.Count;
\r
65 nativeListStruct.items_count = list.Count;
\r
67 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
68 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
70 returnList.ListPtr = nativeListStructPtr;
\r
75 /// Creates a native HandBrake list from the given managed list of structures.
\r
77 /// <typeparam name="T">The type of structures in the list.</typeparam>
\r
78 /// <param name="list">The managed list to convert.</param>
\r
79 /// <returns>The converted native list.</returns>
\r
80 public static NativeList ConvertListBack<T>(List<T> list)
\r
82 NativeList returnList = new NativeList();
\r
83 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
85 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
86 returnList.AllocatedMemory.Add(nativeListInternal);
\r
87 for (int i = 0; i < list.Count; i++)
\r
89 IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));
\r
90 returnList.AllocatedMemory.Add(itemPtr);
\r
91 Marshal.StructureToPtr(list[i], itemPtr, false);
\r
93 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);
\r
96 hb_list_s nativeListStruct = new hb_list_s();
\r
97 nativeListStruct.items = nativeListInternal;
\r
98 nativeListStruct.items_alloc = list.Count;
\r
99 nativeListStruct.items_count = list.Count;
\r
101 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
102 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
104 returnList.ListPtr = nativeListStructPtr;
\r
109 /// Frees all the memory locations in the given list.
\r
111 /// <param name="memoryList">The list of memory locations to free.</param>
\r
112 public static void FreeMemory(List<IntPtr> memoryList)
\r
114 foreach (IntPtr memoryLocation in memoryList)
\r
116 Marshal.FreeHGlobal(memoryLocation);
\r