OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / interop / InteropUtilities.cs
1 namespace HandBrake.Interop\r
2 {\r
3     using System;\r
4     using System.Collections.Generic;\r
5     using System.Runtime.InteropServices;\r
6 \r
7     /// <summary>\r
8     /// Helper utilities for native interop.\r
9     /// </summary>\r
10     public static class InteropUtilities\r
11     {\r
12         /// <summary>\r
13         /// Reads the given native structure pointer.\r
14         /// </summary>\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
19         {\r
20             return (T)Marshal.PtrToStructure(structPtr, typeof(T));\r
21         }\r
22 \r
23         /// <summary>\r
24         /// Converts the given native HandBrake list to a managed list.\r
25         /// </summary>\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
30         {\r
31             List<T> returnList = new List<T>();\r
32             hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);\r
33 \r
34             for (int i = 0; i < itemList.items_count; i++)\r
35             {\r
36                 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));\r
37                 returnList.Add(ReadStructure<T>(itemPtr));\r
38             }\r
39 \r
40             return returnList;\r
41         }\r
42 \r
43         /// <summary>\r
44         /// Creates a native HandBrake list from the given managed list of pointers.\r
45         /// </summary>\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
49         {\r
50             NativeList returnList = new NativeList();\r
51             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
52 \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
56             {\r
57                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);\r
58             }\r
59 \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
64 \r
65             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
66             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
67 \r
68             returnList.ListPtr = nativeListStructPtr;\r
69             return returnList;\r
70         }\r
71 \r
72         /// <summary>\r
73         /// Creates a native HandBrake list from the given managed list of structures.\r
74         /// </summary>\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
79         {\r
80             NativeList returnList = new NativeList();\r
81             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
82 \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
86             {\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
90 \r
91                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);\r
92             }\r
93 \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
98 \r
99             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
100             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
101 \r
102             returnList.ListPtr = nativeListStructPtr;\r
103             return returnList;\r
104         }\r
105 \r
106         /// <summary>\r
107         /// Frees all the memory locations in the given list.\r
108         /// </summary>\r
109         /// <param name="memoryList">The list of memory locations to free.</param>\r
110         public static void FreeMemory(List<IntPtr> memoryList)\r
111         {\r
112             foreach (IntPtr memoryLocation in memoryList)\r
113             {\r
114                 Marshal.FreeHGlobal(memoryLocation);\r
115             }\r
116         }\r
117     }\r
118 }\r