using System; using System.Drawing; using System.Runtime.InteropServices; internal class GCBuffer : SafeBuffer { protected GCHandle gch; public GCBuffer(object value) : base(true) { gch = GCHandle.Alloc(value, GCHandleType.Pinned); handle = gch.AddrOfPinnedObject(); } protected override bool ReleaseHandle() { gch.Free(); return !gch.IsAllocated; } public static void Main(string[] args) { var bytes = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, }; using(var buffer = new GCBuffer(bytes)) { buffer.Initialize((uint)bytes.Length); Console.WriteLine(buffer.Read(0ul)); buffer.Write(0ul, new Point(-1, -2)); buffer.ReadArray(0ul, bytes, 0, bytes.Length); Console.WriteLine(BitConverter.ToString(bytes)); } } }