Class MemoryHelper
A simple class that provides read/write access to arbitrary memory.
Assembly: Dalamud.dll
public static class MemoryHelper
Methods
Cast<T>(nint)
Casts the given memory address as the reference to the live object.
public static ref T Cast<T>(nint memoryAddress) where T : unmanaged
Returns
<T>: The reference to the live object.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address. |
Type Parameters
| Name | Description |
|---|---|
T | The unmanaged type. |
Cast<T>(nint, int)
Casts the given memory address as the span of the live object(s).
public static Span<T> Cast<T>(nint memoryAddress, int length) where T : unmanaged
Returns
System.Span<<T>>: The span containing reference to the live object(s).
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address. |
System.Int32 | length | The number of items. |
Type Parameters
| Name | Description |
|---|---|
T | The unmanaged type. |
CastNullTerminated<T>(nint, int)
Casts the given memory address as the span of the live object(s), until it encounters a zero.
public static Span<T> CastNullTerminated<T>(nint memoryAddress, int maxLength = 2147483647) where T : unmanaged, IEquatable<T>
Returns
System.Span<<T>>: The span containing reference to the live object(s).
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address. |
System.Int32 | maxLength | The maximum number of items. |
Type Parameters
| Name | Description |
|---|---|
T | The unmanaged type. |
Remarks
If <code class="typeparamref">T</code> is byte or char and <code class="paramref">maxLength</code> is not
specified, consider using System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpanFromNullTerminated(System.Byte*) or
System.Runtime.InteropServices.MemoryMarshal.CreateReadOnlySpanFromNullTerminated(System.Char*).
Read<T>(nint)
Reads a generic type from a specified memory address.
public static T Read<T>(nint memoryAddress) where T : unmanaged
Returns
<T>: The read in struct.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr) instead.
Read<T>(nint, bool)
Reads a generic type from a specified memory address.
public static T Read<T>(nint memoryAddress, bool marshal)
Returns
<T>: The read in struct.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Boolean | marshal | Set this to true to enable struct marshalling. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy and <code class="paramref">marshal</code> is false,
use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr) instead.
ReadRaw(nint, int)
Reads a byte array from a specified memory address.
public static byte[] ReadRaw(nint memoryAddress, int length)
Returns
System.Byte[]: The read in byte array.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | length | The amount of bytes to read starting from the memoryAddress. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
Read<T>(nint, int)
Reads a generic type array from a specified memory address.
public static T[] Read<T>(nint memoryAddress, int arrayLength) where T : unmanaged
Returns
<T>[]: The read in struct array.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | arrayLength | The amount of array items to read. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
Read<T>(nint, int, bool)
Reads a generic type array from a specified memory address.
public static T[] Read<T>(nint memoryAddress, int arrayLength, bool marshal)
Returns
<T>[]: The read in struct array.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | arrayLength | The amount of array items to read. |
System.Boolean | marshal | Set this to true to enable struct marshalling. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy and <code class="paramref">marshal</code> is false,
use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
ReadRawNullTerminated(nint)
Reads a null-terminated byte array from a specified memory address.
public static byte[] ReadRawNullTerminated(nint memoryAddress)
Returns
System.Byte[]: The read in byte array.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.CastNullTerminated``1(System.IntPtr,System.Int32) instead.
Read<T>(nint, out T)
Reads a generic type from a specified memory address.
public static void Read<T>(nint memoryAddress, out T value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
<T> | value | Local variable to receive the read in struct. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr) instead.
Read<T>(nint, out T, bool)
Reads a generic type from a specified memory address.
public static void Read<T>(nint memoryAddress, out T value, bool marshal)
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
<T> | value | Local variable to receive the read in struct. |
System.Boolean | marshal | Set this to true to enable struct marshalling. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy and <code class="paramref">marshal</code> is false,
use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr) instead.
ReadRaw(nint, int, out byte[])
Reads raw data from a specified memory address.
public static void ReadRaw(nint memoryAddress, int length, out byte[] value)
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | length | The amount of bytes to read starting from the memoryAddress. |
System.Byte[] | value | Local variable to receive the read in bytes. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
Read<T>(nint, int, out T[])
Reads a generic type array from a specified memory address.
public static void Read<T>(nint memoryAddress, int arrayLength, out T[] value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | arrayLength | The amount of array items to read. |
<T>[] | value | The read in struct array. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy, use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
Read<T>(nint, int, bool, out T[])
Reads a generic type array from a specified memory address.
public static void Read<T>(nint memoryAddress, int arrayLength, bool marshal, out T[] value)
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | arrayLength | The amount of array items to read. |
System.Boolean | marshal | Set this to true to enable struct marshalling. |
<T>[] | value | The read in struct array. |
Type Parameters
| Name | Description |
|---|---|
T | An individual struct type of a class with an explicit StructLayout.LayoutKind attribute. |
Remarks
If you do not need to make a copy and <code class="paramref">marshal</code> is false,
use Dalamud.Memory.MemoryHelper.Cast``1(System.IntPtr,System.Int32) instead.
EqualsZeroTerminatedString(ReadOnlySpan<char>, nint, Encoding?, int)
Compares if the given char span equals to the null-terminated string at <code class="paramref">memoryAddress</code>.
public static bool EqualsZeroTerminatedString(ReadOnlySpan<char> charSpan, nint memoryAddress, Encoding? encoding = null, int maxLength = 2147483647)
Returns
System.Boolean: Whether they are equal.
Parameters
| Type | Name | Description |
|---|---|---|
System.ReadOnlySpan<System.Char> | charSpan | The character span. |
System.IntPtr | memoryAddress | The address of null-terminated string. |
System.Text.Encoding | encoding | The encoding of the null-terminated string. |
System.Int32 | maxLength | The maximum length of the null-terminated string. |
EqualsZeroTerminatedWideString(scoped ReadOnlySpan<char>, nint)
Compares a UTF-16 character span with a null-terminated UTF-16 string at <code class="paramref">memoryAddress</code>.
public static bool EqualsZeroTerminatedWideString(scoped ReadOnlySpan<char> charSpan, nint memoryAddress)
Returns
System.Boolean: true if equal; otherwise, false.
Parameters
| Type | Name | Description |
|---|---|---|
System.ReadOnlySpan<System.Char> | charSpan | UTF-16 character span (e.g., from a string literal). |
System.IntPtr | memoryAddress | Address of null-terminated UTF-16 (wide) string, as used by Windows APIs. |
ReadStringNullTerminated(nint)
Read a UTF-8 encoded string from a specified memory address.
public static string ReadStringNullTerminated(nint memoryAddress)
Returns
System.String: The read in string.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
Remarks
Attention! If this is an Dalamud.Game.Text.SeStringHandling.SeString, use the applicable helper methods to decode.
ReadStringNullTerminated(nint, Encoding)
Read a string with the given encoding from a specified memory address.
public static string ReadStringNullTerminated(nint memoryAddress, Encoding encoding)
Returns
System.String: The read in string.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Text.Encoding | encoding | The encoding to use to decode the string. |
Remarks
Attention! If this is an Dalamud.Game.Text.SeStringHandling.SeString, use the applicable helper methods to decode.
ReadString(nint, int)
Read a UTF-8 encoded string from a specified memory address.
public static string ReadString(nint memoryAddress, int maxLength)
Returns
System.String: The read in string.
Parameters
| Type | Name | Description |
|---|---|---|
System.IntPtr | memoryAddress | The memory address to read from. |
System.Int32 | maxLength | The maximum number of bytes to read. |
Note that this is NOT the maximum length of the returned string. |