I think there might be a bug in the C# method
Marshal.PtrToStructure<T>(IntPtr ptr, T structure)
I wrongly assumed T structure
means you can pass in your structure you want to fill but when I do this as in the example below an exception is thrown:
[System.ArgumentException: The structure must not be a value class. Parameter name: structure]
To give you a simple example:
using System;
using System.Runtime.InteropServices;
public class Program
{
public static void Main()
{
MyStruct s = new MyStruct();
var buffer = new Byte[]{1,2,3};
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
Marshal.PtrToStructure<MyStruct>(handle.AddrOfPinnedObject(), s);
handle.Free();
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct MyStruct
{
public Byte a;
public Byte b;
public Byte c;
}
}
Am I reading the method decleration wrong? Or is there a bug?