fork(2) download
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4.  
  5. internal class GCBuffer : SafeBuffer {
  6. protected GCHandle gch;
  7. public GCBuffer(object value)
  8. : base(true) {
  9. gch = GCHandle.Alloc(value, GCHandleType.Pinned);
  10. handle = gch.AddrOfPinnedObject();
  11. }
  12.  
  13. protected override bool ReleaseHandle() {
  14. gch.Free();
  15. return !gch.IsAllocated;
  16. }
  17.  
  18. public static void Main(string[] args) {
  19. var bytes = new byte[] {
  20. 0x01, 0x00, 0x00, 0x00,
  21. 0x02, 0x00, 0x00, 0x00,
  22. 0x03, 0x00, 0x00, 0x00,
  23. 0x04, 0x00, 0x00, 0x00,
  24. };
  25. using(var buffer = new GCBuffer(bytes)) {
  26. buffer.Initialize<byte>((uint)bytes.Length);
  27. Console.WriteLine(buffer.Read<Rectangle>(0ul));
  28. buffer.Write(0ul, new Point(-1, -2));
  29. buffer.ReadArray(0ul, bytes, 0, bytes.Length);
  30. Console.WriteLine(BitConverter.ToString(bytes));
  31. }
  32. }
  33. }
Success #stdin #stdout 0.04s 24232KB
stdin
Standard input is empty
stdout
{X=1,Y=2,Width=3,Height=4}
FF-FF-FF-FF-FE-FF-FF-FF-03-00-00-00-04-00-00-00