Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy memory with offset in Delphi

I want to copy a memory block with an offset, is it possible?

This is the code I have so far:

const
  SOURCE: array [0..5] of Byte = ($47, $49, $46, $38, $39, $61);
var
  Destination: Pointer;
begin
  // This is a full copy
  Move(SOURCE, Destination^, SizeOf(SOURCE));

  // If i want to copy from the third byte, is it possible?
  // I imagine the code should be, but it cannot be compiled.
  Move(Slice(SOURCE^, {Offset=}2)^, Destination^, SizeOf(SOURCE) - 2);
end;
like image 956
stanleyxu2005 Avatar asked May 20 '12 15:05

stanleyxu2005


2 Answers

It is not entirely clear what you want to achieve, but it looks like

MoveMemory(pointer(NativeUInt(Destination) + 2), @SOURCE[0], SizeOf(SOURCE) - 2)

although I suspect you actually want

MoveMemory(pointer(NativeUInt(Destination) + 2), @SOURCE[2], SizeOf(SOURCE) - 2)
like image 58
Andreas Rejbrand Avatar answered Sep 20 '22 04:09

Andreas Rejbrand


To use Move() to copy a portion of the array, do it like this:

Move(SOURCE[Offset], Destination^, SizeOf(SOURCE)-Offset); 
like image 33
Remy Lebeau Avatar answered Sep 17 '22 04:09

Remy Lebeau