OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / interop / InteropUtilities.cs
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
4 // </copyright>\r
5 // <summary>\r
6 //   Helper utilities for native interop.\r
7 // </summary>\r
8 // --------------------------------------------------------------------------------------------------------------------\r
9 \r
10 namespace HandBrake.Interop\r
11 {\r
12     using System;\r
13     using System.Collections.Generic;\r
14     using System.Runtime.InteropServices;\r
15 \r
16     /// <summary>\r
17     /// Helper utilities for native interop.\r
18     /// </summary>\r
19     public static class InteropUtilities\r
20     {\r
21         /// <summary>\r
22         /// Reads the given native structure pointer.\r
23         /// </summary>\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
28         {\r
29             return (T)Marshal.PtrToStructure(structPtr, typeof(T));\r
30         }\r
31 \r
32         /// <summary>\r
33         /// Converts the given native HandBrake list to a managed list.\r
34         /// </summary>\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
39         {\r
40             List<T> returnList = new List<T>();\r
41             hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);\r
42 \r
43             for (int i = 0; i < itemList.items_count; i++)\r
44             {\r
45                 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));\r
46                 returnList.Add(ReadStructure<T>(itemPtr));\r
47             }\r
48 \r
49             return returnList;\r
50         }\r
51 \r
52         /// <summary>\r
53         /// Creates a native HandBrake list from the given managed list of pointers.\r
54         /// </summary>\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
58         {\r
59             NativeList returnList = new NativeList();\r
60             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
61 \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
65             {\r
66                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);\r
67             }\r
68 \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
73 \r
74             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
75             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
76 \r
77             returnList.ListPtr = nativeListStructPtr;\r
78             return returnList;\r
79         }\r
80 \r
81         /// <summary>\r
82         /// Creates a native HandBrake list from the given managed list of structures.\r
83         /// </summary>\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
88         {\r
89             NativeList returnList = new NativeList();\r
90             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
91 \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
95             {\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
99 \r
100                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);\r
101             }\r
102 \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
107 \r
108             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
109             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
110 \r
111             returnList.ListPtr = nativeListStructPtr;\r
112             return returnList;\r
113         }\r
114 \r
115         /// <summary>\r
116         /// Frees all the memory locations in the given list.\r
117         /// </summary>\r
118         /// <param name="memoryList">The list of memory locations to free.</param>\r
119         public static void FreeMemory(List<IntPtr> memoryList)\r
120         {\r
121             foreach (IntPtr memoryLocation in memoryList)\r
122             {\r
123                 Marshal.FreeHGlobal(memoryLocation);\r
124             }\r
125         }\r
126     }\r
127 }\r