MemoryBlock.Ptr

From Xojo Documentation

Method

MemoryBlock.Ptr(Offset as Integer) As Ptr

Supported for all project types and targets.

Gets or sets a Ptr. Offset is in bytes from the beginning of the MemoryBlock. If the Ptr is to a MemoryBlock, it will be of an unknown size when retrieved. Size will be set to -1 but it still can be used to access its data.

Notes

Note the difference between

Var p As Ptr = mb

and

Var p As Ptr = mb.Ptr(n)

The former gets a Ptr to the MemoryBlock itself while the latter retrieves the Ptr that is stored at offset n within the MemoryBlock.

Sample Code

This example stores a Ptr to a MemoryBlock property. If the MemoryBlock property goes out of scope, the Ptr will be invalid when retrieved.

MBProperty = New MemoryBlock(8)
MBProperty.StringValue(0, 8) = "a string"

Var mb As New MemoryBlock(4)
Var p As Ptr = MBProperty
mb.Ptr(0) = p

Because of implicit conversion between a Ptr and MemoryBlock, this can be shortened.

Var mb As New MemoryBlock(4)
mb.Ptr(0) = MBProperty

This code will retrieve the MemoryBlock later.

Var mb1 As MemoryBlock = mb.Ptr(0) // Size will be unknown (-1)
Var s As String = mb1.StringValue(0, 8)

This code will store a Ptrs to two methods.

Var mb As New MemoryBlock(8)
mb.Ptr(0) = AddressOf someMethod
mb.Ptr(4) = AddressOf someOtherMethod