Show / Hide Table of Contents

# Class SafeMemory

Class facilitating safe memory access.

##### Inheritance

System.Object
SafeMemory

##### Inherited Members

System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()

###### Namespace: Dalamud

###### Assembly: Dalamud.dll

##### Syntax

    public static class SafeMemory

##### 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.

### Methods

| Improve this Doc View Source

#### PtrToStructure(IntPtr, Type)

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

##### Declaration

    public static object PtrToStructure(IntPtr addr, Type type)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | addr | The address to read from.
System.Type | type | The type to create.

##### Returns

Type | Description
--- | ---
System.Object | The read object, or null, if it could not be read.

| Improve this Doc View Source

#### PtrToStructure<T>(IntPtr)

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

##### Declaration

    public static T? PtrToStructure<T>(IntPtr addr)
        where T : struct

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | addr | The address to read from.

##### Returns

Type | Description
--- | ---
System.Nullable<T> | The read object, or null, if it could not be read.

##### Type Parameters

Name | Description
--- | ---
T | The type to create.

| Improve this Doc View Source

#### Read<T>(IntPtr, out T)

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

##### Declaration

    public static bool Read<T>(IntPtr address, out T result)
        where T : struct

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to read from.
T | result | The resulting object.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the read succeeded.

##### Type Parameters

Name | Description
--- | ---
T | The type of the struct.

| Improve this Doc View Source

#### Read<T>(IntPtr, Int32)

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

##### Declaration

    public static T[] Read<T>(IntPtr address, int count)
        where T : struct

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to read from.
System.Int32 | count | The length of the array.

##### Returns

Type | Description
--- | ---
T[] | An array of the read objects, or null if any entry of the array failed to read.

##### Type Parameters

Name | Description
--- | ---
T | The type of the struct.

| Improve this Doc View Source

#### ReadBytes(IntPtr, Int32, out Byte[])

Read a byte array from the current process.

##### Declaration

    public static bool ReadBytes(IntPtr address, int count, out byte[] buffer)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to read from.
System.Int32 | count | The amount of bytes to read.
System.Byte[] | buffer | The result buffer.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the read succeeded.

| Improve this Doc View Source

#### ReadString(IntPtr, Int32)

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

##### Declaration

    public static string ReadString(IntPtr address, int maxLength = 256)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to read from.
System.Int32 | maxLength | The maximum length of the string.

##### Returns

Type | Description
--- | ---
System.String | The read string, or null in case the read was not successful.

##### 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.

| Improve this Doc View Source

#### ReadString(IntPtr, Encoding, Int32)

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

##### Declaration

    public static string ReadString(IntPtr address, Encoding encoding, int maxLength = 256)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to read from.
System.Text.Encoding | encoding | The encoding to use to decode the string.
System.Int32 | maxLength | The maximum length of the string.

##### Returns

Type | Description
--- | ---
System.String | The read string, or null in case the read was not successful.

##### 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.

| Improve this Doc View Source

#### SizeOf<T>()

Get the size of the passed type.

##### Declaration

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

##### Returns

Type | Description
--- | ---
System.Int32 | The size of the passed type.

##### Type Parameters

Name | Description
--- | ---
T | The type to inspect.

| Improve this Doc View Source

#### Write<T>(IntPtr, T)

Write a struct to the current process.

##### Declaration

    public static bool Write<T>(IntPtr address, T obj)
        where T : struct

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to write to.
T | obj | The object to write.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the write succeeded.

##### Type Parameters

Name | Description
--- | ---
T | The type of the struct.

| Improve this Doc View Source

#### Write<T>(IntPtr, T[])

Write an array of structs to the current process.

##### Declaration

    public static bool Write<T>(IntPtr address, T[] objArray)
        where T : struct

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to write to.
T[] | objArray | The array to write.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the write succeeded.

##### Type Parameters

Name | Description
--- | ---
T | The type of the structs.

| Improve this Doc View Source

#### WriteBytes(IntPtr, Byte[])

Write a byte array to the current process.

##### Declaration

    public static bool WriteBytes(IntPtr address, byte[] buffer)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to write to.
System.Byte[] | buffer | The buffer to write.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the write succeeded.

| Improve this Doc View Source

#### WriteString(IntPtr, String)

Write a string to the current process.

##### Declaration

    public static bool WriteString(IntPtr address, string str)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to write to.
System.String | str | The string to write.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the write succeeded.

##### 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.

| Improve this Doc View Source

#### WriteString(IntPtr, String, Encoding)

Write a string to the current process.

##### Declaration

    public static bool WriteString(IntPtr address, string str, Encoding encoding)

##### Parameters

Type | Name | Description
--- | --- | ---
System.IntPtr | address | The address to write to.
System.String | str | The string to write.
System.Text.Encoding | encoding | The encoding to use.

##### Returns

Type | Description
--- | ---
System.Boolean | Whether or not the write succeeded.

##### 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.