From 399ab292d7feddf5e83be866caafbaef634eca87 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sun, 12 Jun 2011 16:54:23 +0000 Subject: WinGui: Bring in the HandBrake Interop library written by RandomEngy. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4045 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../HandBrakeInterop/InteropUtilities.cs | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 win/CS/HandBrake.Interop/HandBrakeInterop/InteropUtilities.cs (limited to 'win/CS/HandBrake.Interop/HandBrakeInterop/InteropUtilities.cs') diff --git a/win/CS/HandBrake.Interop/HandBrakeInterop/InteropUtilities.cs b/win/CS/HandBrake.Interop/HandBrakeInterop/InteropUtilities.cs new file mode 100644 index 000000000..4102e5eb7 --- /dev/null +++ b/win/CS/HandBrake.Interop/HandBrakeInterop/InteropUtilities.cs @@ -0,0 +1,144 @@ +namespace HandBrake.Interop +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Runtime.InteropServices; + + /// + /// Helper utilities for native interop. + /// + public static class InteropUtilities + { + /// + /// Reads the given native structure pointer. + /// + /// The type to convert the structure to. + /// The pointer to the native structure. + /// The converted structure. + public static T ReadStructure(IntPtr structPtr) + { + return (T)Marshal.PtrToStructure(structPtr, typeof(T)); + } + + /// + /// Converts the given native HandBrake list to a managed list. + /// + /// The type of structure in the list. + /// The pointer to the native list. + /// The converted managed list. + public static List ConvertList(IntPtr listPtr) + { + List returnList = new List(); + hb_list_s itemList = ReadStructure(listPtr); + + for (int i = 0; i < itemList.items_count; i++) + { + IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr))); + returnList.Add(ReadStructure(itemPtr)); + } + + return returnList; + } + + /// + /// Creats a new, empty native HandBrake list. + /// + /// The new native list. + public static NativeList CreateNativeList(int capacity) + { + NativeList returnList = new NativeList(); + int intSize = Marshal.SizeOf(typeof(IntPtr)); + + IntPtr nativeListInternal = Marshal.AllocHGlobal(capacity * intSize); + returnList.AllocatedMemory.Add(nativeListInternal); + + hb_list_s nativeListStruct = new hb_list_s(); + nativeListStruct.items = nativeListInternal; + nativeListStruct.items_alloc = capacity; + nativeListStruct.items_count = 0; + + IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s))); + Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false); + + returnList.ListPtr = nativeListStructPtr; + return returnList; + } + + /// + /// Creates a native HandBrake list from the given managed list of pointers. + /// + /// The managed list to convert. + /// The converted native list. + public static NativeList CreateIntPtrList(List list) + { + NativeList returnList = new NativeList(); + int intSize = Marshal.SizeOf(typeof(IntPtr)); + + IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize); + returnList.AllocatedMemory.Add(nativeListInternal); + for (int i = 0; i < list.Count; i++) + { + Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]); + } + + hb_list_s nativeListStruct = new hb_list_s(); + nativeListStruct.items = nativeListInternal; + nativeListStruct.items_alloc = list.Count; + nativeListStruct.items_count = list.Count; + + IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s))); + Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false); + + returnList.ListPtr = nativeListStructPtr; + return returnList; + } + + /// + /// Creates a native HandBrake list from the given managed list of structures. + /// + /// The type of structures in the list. + /// The managed list to convert. + /// The converted native list. + public static NativeList ConvertListBack(List list) + { + NativeList returnList = new NativeList(); + int intSize = Marshal.SizeOf(typeof(IntPtr)); + + IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize); + returnList.AllocatedMemory.Add(nativeListInternal); + for (int i = 0; i < list.Count; i++) + { + IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T))); + returnList.AllocatedMemory.Add(itemPtr); + Marshal.StructureToPtr(list[i], itemPtr, false); + + Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr); + } + + hb_list_s nativeListStruct = new hb_list_s(); + nativeListStruct.items = nativeListInternal; + nativeListStruct.items_alloc = list.Count; + nativeListStruct.items_count = list.Count; + + IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s))); + Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false); + + returnList.ListPtr = nativeListStructPtr; + return returnList; + } + + /// + /// Frees all the memory locations in the given list. + /// + /// The list of memory locations to free. + public static void FreeMemory(List memoryList) + { + foreach (IntPtr memoryLocation in memoryList) + { + Marshal.FreeHGlobal(memoryLocation); + } + } + } +} -- cgit v1.2.3