1 // --------------------------------------------------------------------------------------------------------------------
\r
2 // <copyright file="InteropUtilities.cs" company="HandBrake Project (http://handbrake.fr)">
\r
3 // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
\r
6 // Helper utilities for native interop.
\r
8 // --------------------------------------------------------------------------------------------------------------------
\r
10 namespace HandBrake.Interop
\r
13 using System.Collections.Generic;
\r
14 using System.Runtime.InteropServices;
\r
17 /// Helper utilities for native interop.
\r
19 public static class InteropUtilities
\r
22 /// Reads the given native structure pointer.
\r
24 /// <typeparam name="T">The type to convert the structure to.</typeparam>
\r
25 /// <param name="structPtr">The pointer to the native structure.</param>
\r
26 /// <returns>The converted structure.</returns>
\r
27 public static T ReadStructure<T>(IntPtr structPtr)
\r
29 return (T)Marshal.PtrToStructure(structPtr, typeof(T));
\r
33 /// Converts the given native HandBrake list to a managed list.
\r
35 /// <typeparam name="T">The type of structure in the list.</typeparam>
\r
36 /// <param name="listPtr">The pointer to the native list.</param>
\r
37 /// <returns>The converted managed list.</returns>
\r
38 public static List<T> ConvertList<T>(IntPtr listPtr)
\r
40 List<T> returnList = new List<T>();
\r
41 hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);
\r
43 for (int i = 0; i < itemList.items_count; i++)
\r
45 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));
\r
46 returnList.Add(ReadStructure<T>(itemPtr));
\r
53 /// Creates a native HandBrake list from the given managed list of pointers.
\r
55 /// <param name="list">The managed list to convert.</param>
\r
56 /// <returns>The converted native list.</returns>
\r
57 public static NativeList CreateIntPtrList(List<IntPtr> list)
\r
59 NativeList returnList = new NativeList();
\r
60 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
62 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
63 returnList.AllocatedMemory.Add(nativeListInternal);
\r
64 for (int i = 0; i < list.Count; i++)
\r
66 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);
\r
69 hb_list_s nativeListStruct = new hb_list_s();
\r
70 nativeListStruct.items = nativeListInternal;
\r
71 nativeListStruct.items_alloc = list.Count;
\r
72 nativeListStruct.items_count = list.Count;
\r
74 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
75 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
77 returnList.ListPtr = nativeListStructPtr;
\r
82 /// Creates a native HandBrake list from the given managed list of structures.
\r
84 /// <typeparam name="T">The type of structures in the list.</typeparam>
\r
85 /// <param name="list">The managed list to convert.</param>
\r
86 /// <returns>The converted native list.</returns>
\r
87 public static NativeList ConvertListBack<T>(List<T> list)
\r
89 NativeList returnList = new NativeList();
\r
90 int intSize = Marshal.SizeOf(typeof(IntPtr));
\r
92 IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);
\r
93 returnList.AllocatedMemory.Add(nativeListInternal);
\r
94 for (int i = 0; i < list.Count; i++)
\r
96 IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));
\r
97 returnList.AllocatedMemory.Add(itemPtr);
\r
98 Marshal.StructureToPtr(list[i], itemPtr, false);
\r
100 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);
\r
103 hb_list_s nativeListStruct = new hb_list_s();
\r
104 nativeListStruct.items = nativeListInternal;
\r
105 nativeListStruct.items_alloc = list.Count;
\r
106 nativeListStruct.items_count = list.Count;
\r
108 IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));
\r
109 Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);
\r
111 returnList.ListPtr = nativeListStructPtr;
\r
116 /// Frees all the memory locations in the given list.
\r
118 /// <param name="memoryList">The list of memory locations to free.</param>
\r
119 public static void FreeMemory(List<IntPtr> memoryList)
\r
121 foreach (IntPtr memoryLocation in memoryList)
\r
123 Marshal.FreeHGlobal(memoryLocation);
\r