UntypedPointer

Top  Previous  Next

Pretranslated C# code > Pointers > UntypedPointer

`UntypedPointer` is the runtime's general byte-addressed pointer view. It is the closest representation of an untyped Delphi address and is the common conversion layer between `Pointer`, `Pointer<T>`, and `PChar`.

 

public struct UntypedPointer : IPointer<byte>

 

Backing sources

 

An `UntypedPointer` can refer to:

 

a managed byte array
another one-dimensional managed array
a Delphi `DynamicArray<T>`
owned or borrowed native memory
a writable UTF-16 copy of a C# string
a read-only non-copying view of a C# string

 

Examples:

 

UntypedPointer bytes = new UntypedPointer(byteArray);

UntypedPointer element = new UntypedPointer(intArray, elementIndex);

UntypedPointer native = new UntypedPointer(address, size, ownsMemory: false);

 

For `Array` and `IDynamicArray` constructors, the second argument is an element index. Use `FromByteIndex` when the supplied offset is already expressed in bytes:

 

UntypedPointer p1 = new UntypedPointer(values, elementIndex);

UntypedPointer p2 = UntypedPointer.FromByteIndex(values, byteOffset);

 

 

Byte access

 

Arithmetic, indexed access, `Inc`, and `Dec` are byte-based:

 

byte value = p.Deref();

p.Assign(0x20);

p[4] = 0xFF;

p++;

p = p + 8;

 

Typed access

 

`Read<T>` and `Write<T>` interpret their index as a byte offset:

 

int value = p.Read<int>();

p.Write(value);

 

ushort word = p.Read<ushort>(4);

p.Write(word, 4);

 

 

Convert an untyped view to a typed view with `As<T>()`:

 

Pointer<TRecord> record = p.As<TRecord>();

 

The conversion creates a view and does not copy data.

 

String factories

 

`FromValue(string)` creates a writable UTF-16 copy with a null terminator:

 

UntypedPointer writableCopy =

    UntypedPointer.FromValue(text);

 

 

Changes do not automatically modify the original C# string.

 

`FromStringView(string)` creates a non-copying, read-only UTF-16 view:

 

UntypedPointer readOnly =

    UntypedPointer.FromStringView(text);

 

Writing through this pointer throws `InvalidOperationException`.

 

`FromValue<T>(value)` creates a separate one-element backing. It must not be used to implement the address of a local variable or an untyped `var` parameter:

 

UntypedPointer copy = UntypedPointer.FromValue(value);

 

For an addressable local variable, Delphi2C# uses a helper such as `DelphiCell<T>`.

 

Native allocation

 

`AllocBytes(size)` creates managed byte-array storage.

 

`AllocNative(size)` creates owned native memory.

 

UntypedPointer managed = UntypedPointer.AllocBytes(256);

UntypedPointer native = UntypedPointer.AllocNative(256);

 

Owned native storage must be released with `DelphiMemory.FreeMem(ref pointer)`.

 

Conversion to `IntPtr`

 

`ToIntPtr()` returns the current address. Managed byte-addressable backing is pinned as needed.

 

For a scoped native call, prefer `Pin()`:

 

using PointerPin pin = p.Pin();

NativeFunction(pin.Address);

 

 

Equality

 

Pointer equality compares the backing identity and current byte position. It does not compare pointed-to contents or view length.

 

Two separately wrapped native pointers may have the same numeric `IntPtr` address but different backing identities. Compare their `IntPtr` values when numeric native-address equality is specifically required.

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content