You should be able to use the pointer to get the elements one by one. Take a look at this post: Getting Array of struct from IntPtr.
I also managed to do it using RtlMoveMemory, and Buffer.CopyMemory.
Example 1 - Using RrlMoveMemory
I also managed to do it using RtlMoveMemory:
using System.Runtime.InteropServices;
namespace SampleConsoleApp
{
internal static class Program
{
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", ExactSpelling = true)]
public static extern void RtlMoveMemory(IntPtr dest, IntPtr src, int count);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x, y;
}
[STAThread]
static void Main()
{
var srcPtr = GetPOINTs();
var destArray = new POINT[3];
var size = Marshal.SizeOf(typeof(POINT)) * destArray.Length;
var destPtr = Marshal.UnsafeAddrOfPinnedArrayElement(destArray, 0);
RtlMoveMemory(destPtr, srcPtr, size);
foreach (var point in destArray)
Console.WriteLine($"x:{point.x}, y:{point.y}");
Console.ReadLine();
}
static IntPtr GetPOINTs()
{
var array = new[] {
new POINT { x = 1, y = 1 },
new POINT { x = 2, y = 2 },
new POINT { x = 3, y = 3 },
};
return Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
}
}
}
Example 2 - Using Buffer.CopyMemory
I also managed to do it using Buffer.CopyMemory which requires unsafe context:
using System.Runtime.InteropServices;
namespace SampleConsoleApp
{
internal static class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x, y;
}
[STAThread]
static unsafe void Main()
{
var srcPtr = GetPOINTs();
var destArray = new POINT[3];
var size = Marshal.SizeOf(typeof(POINT)) * destArray.Length;
var destPtr = Marshal.UnsafeAddrOfPinnedArrayElement(destArray, 0);
Buffer.MemoryCopy(srcPtr.ToPointer(), destPtr.ToPointer(), size, size);
foreach (var point in destArray)
Console.WriteLine($"x:{point.x}, y:{point.y}");
Console.ReadLine();
}
static IntPtr GetPOINTs()
{
var array = new[] {
new POINT { x = 1, y = 1 },
new POINT { x = 2, y = 2 },
new POINT { x = 3, y = 3 },
};
return Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
}
}
}