public static void ToBinaryWriterBlock(BinaryWriter bw, ordtype_k s) { byte[] buff = new byte[Marshal.SizeOf(typeof(ordtype_k))]; //Create Buffer GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned); //Hands off GC Marshal.StructureToPtr(s, handle.AddrOfPinnedObject(), false); //Marshal the structure handle.Free(); //Give control of the buffer back to the GC bw.Write(buff); //Write byte array }
public static ordtype_k FromBinaryReaderBlock(BinaryReader br) { byte[] buff = br.ReadBytes(Marshal.SizeOf(typeof(ordtype_k))); //Read byte array GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned); //Make sure that the Garbage Collector doesn't move our buffer ordtype_k s = (ordtype_k)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ordtype_k)); //Marshal the bytes handle.Free(); //Give control of the buffer back to the GC return(s); }