I'm using Delphi 5. I define an array of byte, as follows:
Buffer: Array of BYTE;
How to convert it to a PByte pointer?
A dynamic array is implemented as a pointer, so you can simply type-cast it as-is:
var
Buffer: array of Byte;
P: PByte;
begin
SetLength(Buffer, ...);
P := PByte(Buffer);
...
end;
If you don't want to rely on this implementation detail, you can instead take the memory address of the 1st byte in the array:
P := @Buffer[0];
Buffer[0] doesn't exist.Two ways to do this:
var
P: PByte;
....
P := @Buffer[0]; // no cast, compiler can check type safety
P := PByte(Buffer); // implementation detail, dynamic array variable is pointer to first element
Note that the former leads to a range error if range checking is enabled and the array is empty. Because of this I tend to prefer the latter, although you may criticise the direct pointer cast for lacking type safety.
@Buffer[0].