Acron
Acron

Reputation: 1398

Adding pointers

var Buffer: TMemoryStream

The code:

Move((PByte(Buffer.Memory)+Buffer.Position)^, Buffer.Memory^, Buffer.Size - Buffer.Position);

Unfortunately this is not possible: Operator is not applicable to this type of operand.

So how can I get the starting point of a MemoryBuffer?

Upvotes: 0

Views: 3151

Answers (3)

Kcats
Kcats

Reputation: 884

You can only add/subtract integer from a character pointer. From Delphi help:

You can use the + and - operators to increment and decrement the offset of a character pointer. You can also use - to calculate the difference between the offsets of two character pointers. The following rules apply.

If I is an integer and P is a character pointer, then P + I adds I to the address given by P; that is, it returns a pointer to the address I characters after P. (The expression I + P is equivalent to P + I.) P - I subtracts I from the address given by P; that is, it returns a pointer to the address I characters before P. This is true for PChar pointers; for PWideChar pointers P + I adds SizeOf(WideChar) to P.

If P and Q are both character pointers, then P - Q computes the difference between the address given by P (the higher address) and the address given by Q (the lower address); that is, it returns an integer denoting the number of characters between P and Q. P + Q is not defined.

Try this:

Move((PAnsiChar(Buffer.Memory)+Buffer.Position)^, Buffer.Memory^, Buffer.Size - Buffer.Position);

Upvotes: 5

The_Fox
The_Fox

Reputation: 7062

You are casting Buffer.Memory to PByte and want to add an Int64 value. That doesn't work (Delphi is very strict about what you do with pointers). Try this:

Move(Pointer(Int64(Buffer.Memory)+Buffer.Position)^, Buffer.Memory^, Buffer.Size - Buffer.Position);

This works to:

Move(PAnsiChar(Buffer.Memory)[Buffer.Position], Buffer.Memory^, Buffer.Size - Buffer.Position);

This "should" work in Delphi 2009 with pointermath on:

Move(PByte(Buffer.Memory)[Buffer.Position], Buffer.Memory^, Buffer.Size - Buffer.Position);

Upvotes: 1

Alex
Alex

Reputation: 5668

I think that original code with PByte should work in Delphi 2009, as it now has more types with pointer math enabled.

Upvotes: 2

Related Questions