Skip to main content
Version: 15.x (API 15) [Current]

Class SafeMemory

Class facilitating safe memory access.

Remarks​

Attention! The performance of these methods is severely worse than regular System.Runtime.InteropServices.Marshal calls. Please consider using those instead in performance-critical code.

Assembly: Dalamud.dll​
Declaration
public static class SafeMemory

Methods​

ReadBytes(nint, int, out byte[])​

Read a byte array from the current process.

Declaration
public static bool ReadBytes(nint address, int count, out byte[] buffer)
Returns​

System.Boolean: Whether the read succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to read from.
System.Int32countThe amount of bytes to read.
System.Byte[]bufferThe result buffer.

WriteBytes(nint, byte[])​

Write a byte array to the current process.

Declaration
public static bool WriteBytes(nint address, byte[] buffer)
Returns​

System.Boolean: Whether the write succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to write to.
System.Byte[]bufferThe buffer to write.

Read<T>(nint, out T)​

Read an object of the specified struct from the current process.

Declaration
public static bool Read<T>(nint address, out T result) where T : struct
Returns​

System.Boolean: Whether the read succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to read from.
<T>resultThe resulting object.
Type Parameters​
NameDescription
TThe type of the struct.

Read<T>(nint, int)​

Read an array of objects of the specified struct from the current process.

Declaration
public static T[]? Read<T>(nint address, int count) where T : struct
Returns​

<T>[]: An array of the read objects, or null if any entry of the array failed to read.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to read from.
System.Int32countThe length of the array.
Type Parameters​
NameDescription
TThe type of the struct.

Write<T>(nint, T)​

Write a struct to the current process.

Declaration
public static bool Write<T>(nint address, T obj) where T : struct
Returns​

System.Boolean: Whether the write succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to write to.
<T>objThe object to write.
Type Parameters​
NameDescription
TThe type of the struct.

Write<T>(nint, T[])​

Write an array of structs to the current process.

Declaration
public static bool Write<T>(nint address, T[] objArray) where T : struct
Returns​

System.Boolean: Whether the write succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to write to.
<T>[]objArrayThe array to write.
Type Parameters​
NameDescription
TThe type of the structs.

ReadString(nint, int)​

Read a string from the current process(UTF-8).

Declaration
public static string? ReadString(nint address, int maxLength = 256)
Returns​

System.String: The read string, or null in case the read was not successful.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to read from.
System.Int32maxLengthThe maximum length of the string.
Remarks​

Attention! This will use the .NET Encoding.UTF8 class to decode the text. If you read a FFXIV string, please use ReadBytes and parse the string with the applicable class, since Encoding.UTF8 destroys the FFXIV payload structure.

ReadString(nint, Encoding, int)​

Read a string from the current process(UTF-8).

Declaration
public static string? ReadString(nint address, Encoding encoding, int maxLength = 256)
Returns​

System.String: The read string, or null in case the read was not successful.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to read from.
System.Text.EncodingencodingThe encoding to use to decode the string.
System.Int32maxLengthThe maximum length of the string.
Remarks​

Attention! This will use the .NET Encoding class to decode the text. If you read a FFXIV string, please use ReadBytes and parse the string with the applicable class, since Encoding may destroy the FFXIV payload structure.

WriteString(nint, string)​

Write a string to the current process.

Declaration
public static bool WriteString(nint address, string str)
Returns​

System.Boolean: Whether the write succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to write to.
System.StringstrThe string to write.
Remarks​

Attention! This will use the .NET Encoding class to encode the text. If you read a FFXIV string, please use WriteBytes with the applicable encoded SeString, since Encoding may destroy the FFXIV payload structure.

WriteString(nint, string, Encoding)​

Write a string to the current process.

Declaration
public static bool WriteString(nint address, string str, Encoding encoding)
Returns​

System.Boolean: Whether the write succeeded.

Parameters​
TypeNameDescription
System.IntPtraddressThe address to write to.
System.StringstrThe string to write.
System.Text.EncodingencodingThe encoding to use.
Remarks​

Attention! This will use the .NET Encoding class to encode the text. If you read a FFXIV string, please use WriteBytes with the applicable encoded SeString, since Encoding may destroy the FFXIV payload structure.

PtrToStructure<T>(nint)​

Marshals data from an unmanaged block of memory to a managed object.

Declaration
public static T? PtrToStructure<T>(nint addr) where T : struct
Returns​

System.Nullable<<T>>: The read object, or null, if it could not be read.

Parameters​
TypeNameDescription
System.IntPtraddrThe address to read from.
Type Parameters​
NameDescription
TThe type to create.

PtrToStructure(nint, Type)​

Marshals data from an unmanaged block of memory to a managed object.

Declaration
public static object? PtrToStructure(nint addr, Type type)
Returns​

System.Object: The read object, or null, if it could not be read.

Parameters​
TypeNameDescription
System.IntPtraddrThe address to read from.
System.TypetypeThe type to create.

SizeOf<T>()​

Get the size of the passed type.

Declaration
public static int SizeOf<T>() where T : struct
Returns​

System.Int32: The size of the passed type.

Type Parameters​
NameDescription
TThe type to inspect.