forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarshalHelper.cs
39 lines (33 loc) · 960 Bytes
/
MarshalHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Runtime.InteropServices;
namespace TwoMGFX
{
internal class MarshalHelper
{
public static T Unmarshal<T>(IntPtr ptr)
{
var type = typeof(T);
var result = (T)Marshal.PtrToStructure(ptr, type);
return result;
}
public static T[] UnmarshalArray<T>(IntPtr ptr, int count)
{
var type = typeof(T);
var size = Marshal.SizeOf(type);
var ret = new T[count];
for (int i = 0; i < count; i++)
{
var offset = i * size;
var structPtr = new IntPtr(ptr.ToInt64() + offset);
ret[i] = (T)Marshal.PtrToStructure(structPtr, type);
}
return ret;
}
public static byte[] UnmarshalArray(IntPtr ptr, int count)
{
var result = new byte[count];
Marshal.Copy(ptr, result, 0, count);
return result;
}
}
}