You can do in Delphi exactly what you would do in C++. Define a named type for the record pointer, though, since Delphi doesn't let you define types in statements.
type
PPacket = ^TPacket;
var
packet: PPacket;
packet := PPacket(@data[0]);
The reason I've used @data[0]
is that it works regardless of whether data is a dynamic array. If it's a dynamic array, then data
is really a pointer to the first byte of the packet, so this would work:
packet := PPacket(data); // for dynamic array only
But if data
is not a dynamic array, then that type-cast won't be valid. You'd instead need to type-cast a pointer to data
, like this:
packet := PPacket(@data); // for static array only
That won't work if it's a dynamic array. The first code will work in both cases. But if you have range checking enabled (and you probably should), then I think the first code will raise an exception if data
is a zero-length dynamic array, so be careful.
If you want to go the C# route instead, where you copy the bytes from the array into a separate TPacket
variable, then I'd use this:
var
packet: TPacket;
// Source param comes first. Params are passed by
// reference automatically.
Move(data[0], packet, SizeOf(packet));
You'll need to ensure that data
contains enough bytes to fill an entire TPacket
value. TPacket
had better not contain any compiler-managed types, like string
, Variant
, IUnknown
, or dynamic-array types. Neither pointer type-casting nor Move
behave well when that's the case.