Delphi pointer arithmetic
How should this line of code be writed to allow it to compile
MoveMemory(poleFileDescriptorW
, (oleDataPointer + SizeOf(oleFileDescriptorW) *Index + 4)^
, SizeOf(oleFileDescriptorW));
Particularly this part
(oleDataPointer + SizeOf(oleFileDescriptorW)*Index + 4)^
I am just want to shift the pointer by SizeOf(oleFil开发者_运维知识库eDescriptorW)*Index + 4
bytes
Variables are defined as:
pOLEFileDescriptorW : ^FILEDESCRIPTORW;
oleDataPointer : Pointer;
Cast to an integer type, do the math and cast back.
I usually used Cardinal
but I think that doesn't work with a 64 bit compiler.
Pointer(NativeInt(oleDataPointer) + SizeOf(oleFileDescriptorW)*Index + 4)
If you're using DELPHI >= 2009, set $POINTERMATH to ON and you can use pointer arithmetic directly
MoveMemory
and the identical CopyMemory
functions accept pointers, not variables, as the Delphi RTL function Move
requires (and this is the only difference between MoveMemory
and Move
).
So you shouldn't dereference the pointer. Just do
MoveMemory(poleFileDescriptorW,
(oleDataPointer + SizeOf(oleFileDescriptorW)*Index + 4),
SizeOf(oleFileDescriptorW));
where each argument is a pointer/cardinal. Depending on your actual data types, you might need to do some trivial casting. For instance, you might need to do PSomeType(cardinal(myPointer) + cardinal(myPointer2))
.
Anyhow, if you would sometime need to dereference a pointer, you need to specify its type.
(oleDataPointer + SizeOf(oleFileDescriptorW)*Index + 4)^
couldn't possibly work (why?). Do
PMyType(cardinal(oleDataPointer) + SizeOf(oleFileDescriptorW)*Index + 4)^
Typecast to Integer.
Pointer(Integer(oleDataPointer) + SizeOf(oleFileDescriptionW) * Index + 4)
And Andreas was right that you should use pointers directly. See if that works.
God bless
精彩评论