Table of Contents

Class MemoryUtil

Namespace
SharpPluginLoader.Core.Memory
Assembly
SharpPluginLoader.Core.dll

Provides low-level memory reading and writing methods.

public static class MemoryUtil
Inheritance
MemoryUtil
Inherited Members

Methods

AddressOf<T>(ref T)

Gets the address of a given reference.

public static nint AddressOf<T>(ref T value) where T : unmanaged

Parameters

value T

The reference to convert

Returns

nint

An address that points to value

Type Parameters

T

The type of the value

Alloc(long)

Allocates a block of native memory of the specified size.

public static nint Alloc(long size)

Parameters

size long

The size of the block to allocate

Returns

nint

The address of the allocated block

Remarks

Any memory allocated using this method must be freed using either Free(nint) or Free(void*). to avoid memory leaks.

Alloc<T>(long)

Allocates an array of type T[] of the specified size in native memory.

public static T* Alloc<T>(long count = 1) where T : unmanaged

Parameters

count long

The size of the array

Returns

T*

The address of the allocated array

Type Parameters

T

The type of the array

Remarks

Generally, if you need to allocate an array of native memory, you should use Create(int, IAllocator?). If you do use this method, you must free the memory using Free(nint) or Free(void*).

AsPointer<T>(ref T)

Converts a given reference to a pointer.

public static T* AsPointer<T>(ref T value) where T : unmanaged

Parameters

value T

The reference to convert

Returns

T*

A pointer of type T* that points to value

Type Parameters

T

The type of the value

AsRef<T>(T*)

Converts a given pointer to a reference.

public static ref T AsRef<T>(T* address) where T : unmanaged

Parameters

address T*

The pointer to convert

Returns

T

A reference of type T that points to address

Type Parameters

T

The type of the value

AsSpan<T>(long, int)

Creates a span around a native array from a given address and count.

public static Span<T> AsSpan<T>(long address, int count) where T : unmanaged

Parameters

address long

The address of the first element

count int

The length of the array

Returns

Span<T>

A new span that represents the native array

Type Parameters

T

The type of the array

AsSpan<T>(nint, int)

Creates a span around a native array from a given address and count.

public static Span<T> AsSpan<T>(nint address, int count) where T : unmanaged

Parameters

address nint

The address of the first element

count int

The length of the array

Returns

Span<T>

A new span that represents the native array

Type Parameters

T

The type of the array

Copy(long, long, long)

Copies a block of memory from one address to another.

public static void Copy(long source, long destination, long count)

Parameters

source long

The source address

destination long

The destination address

count long

The number of bytes to copy

Copy(nint, nint, long)

Copies a block of memory from one address to another.

public static void Copy(nint source, nint destination, long count)

Parameters

source nint

The source address

destination nint

The destination address

count long

The number of bytes to copy

Copy(void*, void*, long)

Copies a block of memory from one address to another.

public static void Copy(void* source, void* destination, long count)

Parameters

source void*

The source address

destination void*

The destination address

count long

The number of bytes to copy

Copy<T>(T*, T*, int)

Copies count elements of type T from one address to another.

public static void Copy<T>(T* source, T* destination, int count) where T : unmanaged

Parameters

source T*

The source address

destination T*

The destination address

count int

The number of elements to copy

Type Parameters

T

The type of the elements

Free(nint)

Frees a block of native memory.

public static void Free(nint address)

Parameters

address nint

The address of the block to free

Free(void*)

Frees a block of native memory.

public static void Free(void* address)

Parameters

address void*

The address of the block to free

GetRef<T>(long)

Gets a reference to a value of type T from a given address.

public static ref T GetRef<T>(long address) where T : unmanaged

Parameters

address long

The address to read from

Returns

T

A reference to the value read

Type Parameters

T

The type of the value

GetRef<T>(nint)

Gets a reference to a value of type T from a given address.

public static ref T GetRef<T>(nint address) where T : unmanaged

Parameters

address nint

The address to read from

Returns

T

A reference to the value read

Type Parameters

T

The type of the value

ReadArray<T>(long, int)

Reads an array of type T[] from a given address.

public static T[] ReadArray<T>(long address, int count = 1) where T : unmanaged

Parameters

address long

The address to read from

count int

The number of elements to read

Returns

T[]

The array read

Type Parameters

T

The type of the array

Remarks

This method copies the data into managed memory.

ReadArray<T>(nint, int)

Reads an array of type T[] from a given address.

public static T[] ReadArray<T>(nint address, int count = 1) where T : unmanaged

Parameters

address nint

The address to read from

count int

The number of elements to read

Returns

T[]

The array read

Type Parameters

T

The type of the array

Remarks

This method copies the data into managed memory.

ReadBytes(nint, int)

Reads a specified number of bytes from a given address.

public static byte[] ReadBytes(nint address, int count)

Parameters

address nint

The address to read from

count int

The number of bytes to read

Returns

byte[]

The bytes read

ReadPointer<T>(long)

Reads a pointer of type T* from a given address.

public static T* ReadPointer<T>(long address) where T : unmanaged

Parameters

address long

The address to read from

Returns

T*

The pointer read

Type Parameters

T

The type of the pointer

ReadPointer<T>(nint)

Reads a pointer of type T* from a given address.

public static T* ReadPointer<T>(nint address) where T : unmanaged

Parameters

address nint

The address to read from

Returns

T*

The pointer read

Type Parameters

T

The type of the pointer

ReadString(byte*, Encoding?)

Reads a null-terminated string from a given address.

public static string ReadString(byte* address, Encoding? encoding = null)

Parameters

address byte*

The address of the string

encoding Encoding

The encoding to use, or UTF8 if no encoding is given

Returns

string

The string read

ReadString(long, Encoding?)

Reads a null-terminated string from a given address.

public static string ReadString(long address, Encoding? encoding = null)

Parameters

address long

The address of the string

encoding Encoding

The encoding to use, or UTF8 if no encoding is given

Returns

string

The string read

ReadString(nint, Encoding?)

Reads a null-terminated string from a given address.

public static string ReadString(nint address, Encoding? encoding = null)

Parameters

address nint

The address of the string

encoding Encoding

The encoding to use, or UTF8 if no encoding is given

Returns

string

The string read

ReadStructArray<T>(long, int)

Reads an array of structs of type T[] from a given address.

public static T[] ReadStructArray<T>(long address, int count = 1) where T : struct

Parameters

address long

The address to read from

count int

The number of elements to read

Returns

T[]

The array read

Type Parameters

T

The type of the struct

Remarks

This method copies the data into managed memory.

ReadStructArray<T>(nint, int)

Reads an array of structs of type T[] from a given address.

public static T[] ReadStructArray<T>(nint address, int count = 1) where T : struct

Parameters

address nint

The address to read from

count int

The number of elements to read

Returns

T[]

The array read

Type Parameters

T

The type of the struct

Remarks

This method copies the data into managed memory.

ReadStruct<T>(long)

Reads a struct of type T from a given address.

public static T ReadStruct<T>(long address) where T : struct

Parameters

address long

The address to read from

Returns

T

The struct read

Type Parameters

T

The type of the struct

Remarks

This method copies the data into managed memory.

ReadStruct<T>(nint)

Reads a struct of type T from a given address.

public static T ReadStruct<T>(nint address) where T : struct

Parameters

address nint

The address to read from

Returns

T

The struct read

Type Parameters

T

The type of the struct

Remarks

This method copies the data into managed memory.

Read<T>(long)

Reads a value of type T from a given address.

public static T Read<T>(long address) where T : unmanaged

Parameters

address long

The address to read from

Returns

T

The value read

Type Parameters

T

The type of the value

Read<T>(nint)

Reads a value of type T from a given address.

public static T Read<T>(nint address) where T : unmanaged

Parameters

address nint

The address to read from

Returns

T

The value read

Type Parameters

T

The type of the value

Realloc(nint, long)

Reallocates a block of native memory to the specified size.

public static nint Realloc(nint address, long newSize)

Parameters

address nint

The address of the block to reallocate

newSize long

The new size of the block

Returns

nint

The address of the reallocated block

Realloc<T>(T*, long)

Reallocates an array of type T[] to the specified size in native memory.

public static T* Realloc<T>(T* address, long count) where T : unmanaged

Parameters

address T*

The address of the array to reallocate

count long

The new size of the array

Returns

T*

The address of the reallocated array

Type Parameters

T

The type of the array

Remarks

Generally, if you need an array in native memory, you should use a NativeArray<T> with Resize(int).

StringLength(byte*)

Gets the length of a null-terminated string at a given address.

public static int StringLength(byte* address)

Parameters

address byte*

The address of the string

Returns

int

The length of the string

Remarks

This method counts the number of bytes, not the number of characters.

StringLength(long)

Gets the length of a null-terminated string at a given address.

public static int StringLength(long address)

Parameters

address long

The address of the string

Returns

int

The length of the string

Remarks

This method counts the number of bytes, not the number of characters.

StringLength(nint)

Gets the length of a null-terminated string at a given address.

public static int StringLength(nint address)

Parameters

address nint

The address of the string

Returns

int

The length of the string

Remarks

This method counts the number of bytes, not the number of characters.

WriteBytes(nint, byte[])

Writes a specified number of bytes to a given address.

public static void WriteBytes(nint address, byte[] bytes)

Parameters

address nint

The address to write to

bytes byte[]

The bytes to write

Remarks

Do not write to EXE regions using this method. Use WriteBytesSafe(nint, byte[]) instead.

WriteBytesSafe(nint, byte[])

Writes a specified number of bytes to a given address.

public static void WriteBytesSafe(nint address, byte[] bytes)

Parameters

address nint

The address to write to

bytes byte[]

The bytes to write

Remarks

This method changes the memory protection of the page prior to writing, and is thus safe to use on EXE regions.