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.Linq;\r
6     using System.Text;\r
7     using System.Runtime.InteropServices;\r
8 \r
9     /// <summary>\r
10     /// Helper utilities for native interop.\r
11     /// </summary>\r
12     public static class InteropUtilities\r
13     {\r
14         /// <summary>\r
15         /// Reads the given native structure pointer.\r
16         /// </summary>\r
17         /// <typeparam name="T">The type to convert the structure to.</typeparam>\r
18         /// <param name="structPtr">The pointer to the native structure.</param>\r
19         /// <returns>The converted structure.</returns>\r
20         public static T ReadStructure<T>(IntPtr structPtr)\r
21         {\r
22             return (T)Marshal.PtrToStructure(structPtr, typeof(T));\r
23         }\r
24 \r
25         /// <summary>\r
26         /// Converts the given native HandBrake list to a managed list.\r
27         /// </summary>\r
28         /// <typeparam name="T">The type of structure in the list.</typeparam>\r
29         /// <param name="listPtr">The pointer to the native list.</param>\r
30         /// <returns>The converted managed list.</returns>\r
31         public static List<T> ConvertList<T>(IntPtr listPtr)\r
32         {\r
33             List<T> returnList = new List<T>();\r
34             hb_list_s itemList = ReadStructure<hb_list_s>(listPtr);\r
35 \r
36             for (int i = 0; i < itemList.items_count; i++)\r
37             {\r
38                 IntPtr itemPtr = Marshal.ReadIntPtr(itemList.items, i * Marshal.SizeOf(typeof(IntPtr)));\r
39                 returnList.Add(ReadStructure<T>(itemPtr));\r
40             }\r
41 \r
42             return returnList;\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Creates a native HandBrake list from the given managed list of pointers.\r
47         /// </summary>\r
48         /// <param name="list">The managed list to convert.</param>\r
49         /// <returns>The converted native list.</returns>\r
50         public static NativeList CreateIntPtrList(List<IntPtr> list)\r
51         {\r
52             NativeList returnList = new NativeList();\r
53             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
54 \r
55             IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);\r
56             returnList.AllocatedMemory.Add(nativeListInternal);\r
57             for (int i = 0; i < list.Count; i++)\r
58             {\r
59                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, list[i]);\r
60             }\r
61 \r
62             hb_list_s nativeListStruct = new hb_list_s();\r
63             nativeListStruct.items = nativeListInternal;\r
64             nativeListStruct.items_alloc = list.Count;\r
65             nativeListStruct.items_count = list.Count;\r
66 \r
67             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
68             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
69 \r
70             returnList.ListPtr = nativeListStructPtr;\r
71             return returnList;\r
72         }\r
73 \r
74         /// <summary>\r
75         /// Creates a native HandBrake list from the given managed list of structures.\r
76         /// </summary>\r
77         /// <typeparam name="T">The type of structures in the list.</typeparam>\r
78         /// <param name="list">The managed list to convert.</param>\r
79         /// <returns>The converted native list.</returns>\r
80         public static NativeList ConvertListBack<T>(List<T> list)\r
81         {\r
82             NativeList returnList = new NativeList();\r
83             int intSize = Marshal.SizeOf(typeof(IntPtr));\r
84 \r
85             IntPtr nativeListInternal = Marshal.AllocHGlobal(list.Count * intSize);\r
86             returnList.AllocatedMemory.Add(nativeListInternal);\r
87             for (int i = 0; i < list.Count; i++)\r
88             {\r
89                 IntPtr itemPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)));\r
90                 returnList.AllocatedMemory.Add(itemPtr);\r
91                 Marshal.StructureToPtr(list[i], itemPtr, false);\r
92 \r
93                 Marshal.WriteIntPtr(nativeListInternal, i * intSize, itemPtr);\r
94             }\r
95 \r
96             hb_list_s nativeListStruct = new hb_list_s();\r
97             nativeListStruct.items = nativeListInternal;\r
98             nativeListStruct.items_alloc = list.Count;\r
99             nativeListStruct.items_count = list.Count;\r
100 \r
101             IntPtr nativeListStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(hb_list_s)));\r
102             Marshal.StructureToPtr(nativeListStruct, nativeListStructPtr, false);\r
103 \r
104             returnList.ListPtr = nativeListStructPtr;\r
105             return returnList;\r
106         }\r
107 \r
108         /// <summary>\r
109         /// Frees all the memory locations in the given list.\r
110         /// </summary>\r
111         /// <param name="memoryList">The list of memory locations to free.</param>\r
112         public static void FreeMemory(List<IntPtr> memoryList)\r
113         {\r
114             foreach (IntPtr memoryLocation in memoryList)\r
115             {\r
116                 Marshal.FreeHGlobal(memoryLocation);\r
117             }\r
118         }\r
119     }\r
120 }\r