1 namespace HandBrake.Interop
\r
4 using System.Collections.Generic;
\r
5 using System.Runtime.InteropServices;
\r
8 /// Helper utilities for native interop.
\r
10 public static class InteropUtilities
\r
13 /// Reads the given native structure pointer.
\r
15 /// <typeparam name="T">The type to convert the structure to.</typeparam>
\r
16 /// <param name="structPtr">The pointer to the native structure.</param>
\r
17 /// <returns>The converted structure.</returns>
\r
18 public static T ReadStructure<T>(IntPtr structPtr)
\r
20 return (T)Marshal.PtrToStructure(structPtr, typeof(T));
\r
24 /// Converts the given native HandBrake list to a managed list.
\r
26 /// <typeparam name="T">The type of structure in the list.</typeparam>
\r
27 /// <param name="listPtr">The pointer to the native list.</param>
\r
28 /// <returns>The converted managed list.</returns>
\r
29 public static List<T> ConvertList<T>(IntPtr listPtr)
\r
31 List<T> returnList = new List<T>();
\r
32 hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);
\r
34 for (int i = 0; i < itemList.items_count; i++)
\r
36 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));
\r
37 returnList.Add(ReadStructure<T>(itemPtr));
\r
44 /// Creates a native HandBrake list from the given managed list of pointers.
\r
46 /// <param name="list">The managed list to convert.</param>
\r
47 /// <returns>The converted native list.</returns>
\r
48 public static NativeList CreateIntPtrList(List<IntPtr> list)
\r
50 NativeList returnList = new NativeList();
\r
51 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
53 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
54 returnList.AllocatedMemory.Add(nativeListInternal);
\r
55 for (int i = 0; i < list.Count; i++)
\r
57 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);
\r
60 hb_list_s nativeListStruct = new hb_list_s();
\r
61 nativeListStruct.items = nativeListInternal;
\r
62 nativeListStruct.items_alloc = list.Count;
\r
63 nativeListStruct.items_count = list.Count;
\r
65 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
66 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
68 returnList.ListPtr = nativeListStructPtr;
\r
73 /// Creates a native HandBrake list from the given managed list of structures.
\r
75 /// <typeparam name="T">The type of structures in the list.</typeparam>
\r
76 /// <param name="list">The managed list to convert.</param>
\r
77 /// <returns>The converted native list.</returns>
\r
78 public static NativeList ConvertListBack<T>(List<T> list)
\r
80 NativeList returnList = new NativeList();
\r
81 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
83 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
84 returnList.AllocatedMemory.Add(nativeListInternal);
\r
85 for (int i = 0; i < list.Count; i++)
\r
87 IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));
\r
88 returnList.AllocatedMemory.Add(itemPtr);
\r
89 Marshal.StructureToPtr(list[i], itemPtr, false);
\r
91 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);
\r
94 hb_list_s nativeListStruct = new hb_list_s();
\r
95 nativeListStruct.items = nativeListInternal;
\r
96 nativeListStruct.items_alloc = list.Count;
\r
97 nativeListStruct.items_count = list.Count;
\r
99 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
100 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
102 returnList.ListPtr = nativeListStructPtr;
\r
107 /// Frees all the memory locations in the given list.
\r
109 /// <param name="memoryList">The list of memory locations to free.</param>
\r
110 public static void FreeMemory(List<IntPtr> memoryList)
\r
112 foreach (IntPtr memoryLocation in memoryList)
\r
114 Marshal.FreeHGlobal(memoryLocation);
\r