MemoryUtil
Namespace: SharpPluginLoader.Core.Memory
Provides low-level memory reading and writing methods.
public static class MemoryUtil
Inheritance Object → MemoryUtil
Methods
Read<T>(IntPtr)
Reads a value of type from a given address.
public static T Read<T>(nint address)
Type Parameters
T
The type of the value
Parameters
address
IntPtr
The address to read from
Returns
T
The value read
ReadPointer<T>(IntPtr)
Reads a pointer of type * from a given address.
public static T* ReadPointer<T>(nint address)
Type Parameters
T
The type of the pointer
Parameters
address
IntPtr
The address to read from
Returns
T*
The pointer read
GetRef<T>(IntPtr)
Gets a reference to a value of type from a given address.
public static T& GetRef<T>(nint address)
Type Parameters
T
The type of the value
Parameters
address
IntPtr
The address to read from
Returns
T&
A reference to the value read
ReadArray<T>(IntPtr, Int32)
Reads an array of type [] from a given address.
public static T[] ReadArray<T>(nint address, int count)
Type Parameters
T
The type of the array
Parameters
address
IntPtr
The address to read from
count
Int32
The number of elements to read
Returns
T[]
The array read
Remarks:
This method copies the data into managed memory.
ReadStruct<T>(IntPtr)
Reads a struct of type from a given address.
public static T ReadStruct<T>(nint address)
Type Parameters
T
The type of the struct
Parameters
address
IntPtr
The address to read from
Returns
T
The struct read
Remarks:
This method copies the data into managed memory.
ReadStructArray<T>(IntPtr, Int32)
Reads an array of structs of type [] from a given address.
public static T[] ReadStructArray<T>(nint address, int count)
Type Parameters
T
The type of the struct
Parameters
address
IntPtr
The address to read from
count
Int32
The number of elements to read
Returns
T[]
The array read
Remarks:
This method copies the data into managed memory.
Read<T>(Int64)
public static T Read<T>(long address)
Type Parameters
T
Parameters
address
Int64
Returns
T
ReadPointer<T>(Int64)
public static T* ReadPointer<T>(long address)
Type Parameters
T
Parameters
address
Int64
Returns
T*
GetRef<T>(Int64)
public static T& GetRef<T>(long address)
Type Parameters
T
Parameters
address
Int64
Returns
T&
ReadArray<T>(Int64, Int32)
public static T[] ReadArray<T>(long address, int count)
Type Parameters
T
Parameters
address
Int64
count
Int32
Returns
T[]
ReadStruct<T>(Int64)
public static T ReadStruct<T>(long address)
Type Parameters
T
Parameters
address
Int64
Returns
T
ReadStructArray<T>(Int64, Int32)
public static T[] ReadStructArray<T>(long address, int count)
Type Parameters
T
Parameters
address
Int64
count
Int32
Returns
T[]
ReadBytes(IntPtr, Int32)
Reads a specified number of bytes from a given address.
public static Byte[] ReadBytes(nint address, int count)
Parameters
address
IntPtr
The address to read from
count
Int32
The number of bytes to read
Returns
Byte[]
The bytes read
WriteBytes(IntPtr, Byte[])
Writes a specified number of bytes to a given address.
public static void WriteBytes(nint address, Byte[] bytes)
Parameters
address
IntPtr
The address to write to
bytes
Byte[]
The bytes to write
Remarks:
Do not write to EXE regions using this method. Use MemoryUtil.WriteBytesSafe(IntPtr, Byte[]) instead.
WriteBytesSafe(IntPtr, Byte[])
Writes a specified number of bytes to a given address.
public static void WriteBytesSafe(nint address, Byte[] bytes)
Parameters
address
IntPtr
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.
Alloc(Int64)
Allocates a block of native memory of the specified size.
public static nint Alloc(long size)
Parameters
size
Int64
The size of the block to allocate
Returns
IntPtr
The address of the allocated block
Remarks:
Any memory allocated using this method must be freed using either MemoryUtil.Free(IntPtr) or MemoryUtil.Free(IntPtr). to avoid memory leaks.
Realloc(IntPtr, Int64)
Reallocates a block of native memory to the specified size.
public static nint Realloc(nint address, long newSize)
Parameters
address
IntPtr
The address of the block to reallocate
newSize
Int64
The new size of the block
Returns
IntPtr
The address of the reallocated block
Alloc<T>(Int64)
Allocates an array of type [] of the specified size in native memory.
public static T* Alloc<T>(long count)
Type Parameters
T
The type of the array
Parameters
count
Int64
The size of the array
Returns
T*
The address of the allocated array
Remarks:
Generally, if you need to allocate an array of native memory, you should use NativeArray<T>.Create(Int32). If you do use this method, you must free the memory using MemoryUtil.Free(IntPtr) or MemoryUtil.Free(IntPtr).
Realloc<T>(T, Int64)*
Reallocates an array of type [] to the specified size in native memory.
public static T* Realloc<T>(T* address, long count)
Type Parameters
T
The type of the array
Parameters
address
T*
The address of the array to reallocate
count
Int64
The new size of the array
Returns
T*
The address of the reallocated array
Remarks:
Generally, if you need an array in native memory, you should use a NativeArray<T> with NativeArray<T>.Resize(Int32).
Free(IntPtr)
Frees a block of native memory.
public static void Free(nint address)
Parameters
address
IntPtr
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
StringLength(IntPtr)
Gets the length of a null-terminated string at a given address.
public static int StringLength(nint address)
Parameters
address
IntPtr
The address of the string
Returns
Int32
The length of the string
Remarks:
This method counts the number of bytes, not the number of characters.
ReadString(IntPtr, Encoding)
Reads a null-terminated string from a given address.
public static string ReadString(nint address, Encoding encoding)
Parameters
address
IntPtr
The address of the string
encoding
Encoding
The encoding to use, or UTF8 if no encoding is given
Returns
String
The string read
StringLength(Int64)
public static int StringLength(long address)
Parameters
address
Int64
Returns
ReadString(Int64, Encoding)
public static string ReadString(long address, Encoding encoding)
Parameters
address
Int64
encoding
Encoding
Returns
StringLength(Byte)*
public static int StringLength(Byte* address)
Parameters
address
Byte*
Returns
ReadString(Byte, Encoding)*
public static string ReadString(Byte* address, Encoding encoding)
Parameters
address
Byte*
encoding
Encoding
Returns
AsSpan<T>(IntPtr, Int32)
Creates a span around a native array from a given address and count.
public static Span<T> AsSpan<T>(nint address, int count)
Type Parameters
T
The type of the array
Parameters
address
IntPtr
The address of the first element
count
Int32
The length of the array
Returns
Span<T>
A new span that represents the native array
AsSpan<T>(Int64, Int32)
public static Span<T> AsSpan<T>(long address, int count)
Type Parameters
T
Parameters
address
Int64
count
Int32
Returns
Span<T>
AsPointer<T>(T&)
Converts a given reference to a pointer.
public static T* AsPointer<T>(T& value)
Type Parameters
T
The type of the value
Parameters
value
T&
The reference to convert
Returns
T*
A pointer of type * that points to
AsRef<T>(T)*
Converts a given pointer to a reference.
public static T& AsRef<T>(T* address)
Type Parameters
T
The type of the value
Parameters
address
T*
The pointer to convert
Returns
T&
A reference of type that points to
AddressOf<T>(T&)
Gets the address of a given reference.
public static nint AddressOf<T>(T& value)
Type Parameters
T
The type of the value
Parameters
value
T&
The reference to convert
Returns
IntPtr
An address that points to