In C++ it’s done like that:
tPacket * packet = (tPacket *)data; //data is byte[] array; tPacket is a structure
In C#:
tPacket t = new tPacket();
GCHandle pin = GCHandle.Alloc(data, GCHandleType.Pinned);
t = (tPacket)Marshal.PtrToStructure(pin.AddrOfPinnedObject(), typeof(tPacket));
pin.free();
Data is a byte array used as a receive buffer after a packet is received over TCP. That code puts data in an instance of tPacket (a structure) so I can access the structure later.
How is it done in Delphi?
You can also use the absolute keyword to force both structures to share the same memory address:
Any data written to S is also available as Data without having to perform a move or pointer casting.