Class Marshaler
Provides a collection of extended methods that manipulate and extend the functionality of the Marshal class for interoperating with unmanaged memory and libraries.
public static class Marshaler
- Inheritance
-
Marshaler
- Inherited Members
Methods
CopyTo(IntPtr, byte[])
Copies data from an unmanaged pointer to a byte array.
public static void CopyTo(this IntPtr ptr, byte[] buffer)
Parameters
ptr
IntPtrThe pointer to the unmanaged memory to copy from.
buffer
byte[]The byte array to copy the data into.
CopyTo(IntPtr, byte[], int, int)
Copies a specified amount of data from an unmanaged pointer to a byte array.
public static void CopyTo(this IntPtr ptr, byte[] buffer, int offset, int length)
Parameters
ptr
IntPtrThe pointer to the unmanaged memory to copy from.
buffer
byte[]The byte array to copy the data into.
offset
intThe starting index in the buffer where data should be copied.
length
intThe number of bytes to copy.
FreeHGlobal(IntPtr)
Frees an unmanaged memory block previously allocated with AllocHGlobal(IntPtr).
public static void FreeHGlobal(this IntPtr ptr)
Parameters
ptr
IntPtrThe pointer to the unmanaged memory to free.
FreeLibrary(IntPtr)
Frees a previously loaded unmanaged library.
public static bool FreeLibrary(this IntPtr hModule)
Parameters
hModule
IntPtrThe handle to the library to free.
Returns
- bool
true
if the library was successfully freed; otherwise,false
.
FromAnsi(string)
Converts a managed string to an unmanaged ANSI string and returns a pointer to it.
public static IntPtr FromAnsi(this string str)
Parameters
str
stringThe managed string to convert.
Returns
- IntPtr
A pointer to the unmanaged ANSI string.
FromAuto(string)
Converts a managed string to an unmanaged string (platform-dependent encoding) and returns a pointer to it.
public static IntPtr FromAuto(this string str)
Parameters
str
stringThe managed string to convert.
Returns
- IntPtr
A pointer to the unmanaged string.
FromBSTR(string)
Converts a managed string to an unmanaged BSTR and returns a pointer to it.
public static IntPtr FromBSTR(this string str)
Parameters
str
stringThe managed string to convert.
Returns
- IntPtr
A pointer to the unmanaged BSTR.
FromUnicode(string)
Converts a managed string to an unmanaged Unicode string and returns a pointer to it.
public static IntPtr FromUnicode(this string str)
Parameters
str
stringThe managed string to convert.
Returns
- IntPtr
A pointer to the unmanaged Unicode string.
GetDelegateForFunctionPointer<T>(IntPtr)
Converts an unmanaged function pointer to a delegate of the specified type.
public static T GetDelegateForFunctionPointer<T>(this IntPtr ptr)
Parameters
ptr
IntPtrThe unmanaged function pointer to convert.
Returns
- T
A delegate of type
T
that wraps the unmanaged function.
Type Parameters
T
The type of the delegate to create.
GetHandler<T>(IntPtr, string)
Retrieves a delegate for a named procedure from an unmanaged library.
public static T GetHandler<T>(this IntPtr library, string procName)
Parameters
library
IntPtrThe handle to the loaded unmanaged library.
procName
stringThe name of the procedure to retrieve.
Returns
- T
A delegate of type
T
for the specified procedure.
Type Parameters
T
The type of the delegate to retrieve.
Exceptions
- ArgumentException
Thrown when the procedure cannot be found in the library.
GetProcAddress(IntPtr, string)
Retrieves the address of a named procedure from an unmanaged library.
public static IntPtr GetProcAddress(this IntPtr hModule, string procName)
Parameters
hModule
IntPtrThe handle to the loaded unmanaged library.
procName
stringThe name of the procedure to retrieve.
Returns
- IntPtr
The address of the procedure in unmanaged memory.
Exceptions
- ArgumentException
Thrown when the procedure cannot be found in the library.
GetUnsafeString(Encoding, ref byte, int)
Converts an unmanaged byte reference to a managed string using the specified encoding, with a maximum byte length.
public static string GetUnsafeString(this Encoding encoding, ref byte srcChar, int maxBytes)
Parameters
encoding
EncodingThe encoding to use for decoding the string.
srcChar
byteA reference to the starting byte in unmanaged memory.
maxBytes
intThe maximum number of bytes to read.
Returns
- string
The decoded managed string, or
null
if the source is zero.
Exceptions
- ArgumentNullException
Thrown when
encoding
is null.- ArgumentOutOfRangeException
Thrown when
maxBytes
is negative.
LoadLibrary(string)
Loads an unmanaged library from the specified path.
public static IntPtr LoadLibrary(string dllPath)
Parameters
dllPath
stringThe file path to the unmanaged library (DLL).
Returns
- IntPtr
A handle to the loaded library.
Exceptions
- ArgumentNullException
Thrown when
dllPath
is null or empty.- ArgumentException
Thrown when the library cannot be loaded.
Read<T>(IntPtr)
Reads a value from the specified unmanaged memory location.
public static T Read<T>(this IntPtr ptr) where T : struct
Parameters
ptr
IntPtrThe address in unmanaged memory to read from.
Returns
- T
The value read from the unmanaged memory, cast to type
T
.
Type Parameters
T
The type of the value to read. Must be a supported primitive type (e.g., byte, short, int, long, IntPtr).
Exceptions
- ArgumentException
Thrown when
T
is not a supported type.
SetUnsafeString(Encoding, ref byte, int, string)
Writes a managed string to an unmanaged byte reference using the specified encoding, with a maximum byte length.
public static void SetUnsafeString(this Encoding encoding, ref byte tgtChar, int maxBytes, string value)
Parameters
encoding
EncodingThe encoding to use for encoding the string.
tgtChar
byteA reference to the target byte in unmanaged memory.
maxBytes
intThe maximum number of bytes to write.
value
stringThe string to write.
Exceptions
- ArgumentNullException
Thrown when
encoding
is null.- ArgumentOutOfRangeException
Thrown when
maxBytes
is negative or the string length exceedsmaxBytes
.
StructToPtrEx<T>(T, int?)
Marshals data from a managed object to an unmanaged block of memory and returns the pointer along with the size.
public static (IntPtr ptr, int size) StructToPtrEx<T>(this T structure, int? size = null) where T : struct
Parameters
structure
TThe managed object to marshal.
size
int?The optional size of the unmanaged memory block. If null, the size of
T
is used.
Returns
- (IntPtr ptr, int size)
A tuple containing the pointer to the unmanaged memory and its size in bytes.
Type Parameters
T
The type of the structure to marshal. Must be a value type.
StructToPtr<T>(T, int?)
Marshals data from a managed object to an unmanaged block of memory and returns the pointer.
public static IntPtr StructToPtr<T>(this T structure, int? size = null) where T : struct
Parameters
structure
TThe managed object to marshal.
size
int?The optional size of the unmanaged memory block. If null, the size of
T
is used.
Returns
- IntPtr
A pointer to the allocated unmanaged memory containing the marshaled data.
Type Parameters
T
The type of the structure to marshal. Must be a value type.
ToAnsi(IntPtr)
Converts an unmanaged ANSI string pointer to a managed string.
public static string ToAnsi(this IntPtr ptr)
Parameters
ptr
IntPtrThe pointer to the ANSI string in unmanaged memory.
Returns
- string
The managed string representation of the ANSI string.
ToAnsi(IntPtr, int)
Converts an unmanaged ANSI string pointer to a managed string with a specified length.
public static string ToAnsi(this IntPtr ptr, int len)
Parameters
ptr
IntPtrThe pointer to the ANSI string in unmanaged memory.
len
intThe length of the string to read.
Returns
- string
The managed string representation of the ANSI string.
ToAuto(IntPtr)
Converts an unmanaged string pointer (platform-dependent encoding) to a managed string.
public static string ToAuto(this IntPtr ptr)
Parameters
ptr
IntPtrThe pointer to the string in unmanaged memory.
Returns
- string
The managed string representation.
ToAuto(IntPtr, int)
Converts an unmanaged string pointer (platform-dependent encoding) to a managed string with a specified length.
public static string ToAuto(this IntPtr ptr, int len)
Parameters
Returns
- string
The managed string representation.
ToBSTR(IntPtr)
Converts an unmanaged BSTR pointer to a managed string.
public static string ToBSTR(this IntPtr ptr)
Parameters
ptr
IntPtrThe pointer to the BSTR in unmanaged memory.
Returns
- string
The managed string representation of the BSTR.
ToHGlobal(int)
Allocates unmanaged memory of the specified size and wraps it in a safe handle.
public static HGlobalSafeHandle ToHGlobal(this int ptr)
Parameters
ptr
intThe size of the memory to allocate, interpreted as an integer.
Returns
- HGlobalSafeHandle
A HGlobalSafeHandle wrapping the allocated unmanaged memory.
ToHGlobal(IntPtr)
Allocates unmanaged memory of the specified size and wraps it in a safe handle.
public static HGlobalSafeHandle ToHGlobal(this IntPtr ptr)
Parameters
Returns
- HGlobalSafeHandle
A HGlobalSafeHandle wrapping the allocated unmanaged memory.
ToHGlobal(Encoding, string)
Encodes a string using the specified encoding, allocates unmanaged memory for it, and returns a safe handle.
public static HGlobalSafeHandle ToHGlobal(this Encoding encoding, string data)
Parameters
encoding
EncodingThe encoding to use for the string.
data
stringThe string to encode and allocate.
Returns
- HGlobalSafeHandle
A HGlobalSafeHandle wrapping the allocated unmanaged memory containing the encoded string.
Exceptions
- ArgumentNullException
Thrown when
encoding
is null.
ToString(Encoding, IntPtr)
Decodes an unmanaged ANSI string from a pointer into a managed string using the specified encoding.
public static string ToString(this Encoding encoding, IntPtr pData)
Parameters
encoding
EncodingThe encoding to use for decoding the string.
pData
IntPtrThe pointer to the ANSI string in unmanaged memory.
Returns
- string
The decoded managed string.
Exceptions
- ArgumentNullException
Thrown when
encoding
is null.
ToStruct<T>(IntPtr)
Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.
public static T ToStruct<T>(this IntPtr ptr) where T : struct
Parameters
ptr
IntPtrThe pointer to the unmanaged block of memory.
Returns
- T
A managed object of type
T
containing the data from the unmanaged memory.
Type Parameters
T
The type of the structure to marshal. Must be a value type.
ToUnicode(IntPtr)
Converts an unmanaged Unicode string pointer to a managed string.
public static string ToUnicode(this IntPtr ptr)
Parameters
ptr
IntPtrThe pointer to the Unicode string in unmanaged memory.
Returns
- string
The managed string representation of the Unicode string.
ToUnicode(IntPtr, int)
Converts an unmanaged Unicode string pointer to a managed string with a specified length.
public static string ToUnicode(this IntPtr ptr, int len)
Parameters
ptr
IntPtrThe pointer to the Unicode string in unmanaged memory.
len
intThe length of the string to read.
Returns
- string
The managed string representation of the Unicode string.
TryGetHandler<T>(IntPtr, string)
Attempts to retrieve a delegate for a named procedure from an unmanaged library.
public static T TryGetHandler<T>(this IntPtr library, string procName) where T : Delegate
Parameters
library
IntPtrThe handle to the loaded unmanaged library.
procName
stringThe name of the procedure to retrieve.
Returns
- T
A delegate of type
T
if found; otherwise,null
.
Type Parameters
T
The type of the delegate to retrieve. Must inherit from Delegate.
TryGetProcAddress(IntPtr, string, out IntPtr)
Attempts to retrieve the address of a named procedure from an unmanaged library.
public static bool TryGetProcAddress(this IntPtr hModule, string procName, out IntPtr address)
Parameters
hModule
IntPtrThe handle to the loaded unmanaged library.
procName
stringThe name of the procedure to retrieve.
address
IntPtrWhen this method returns, contains the address of the procedure if found; otherwise, Zero.
Returns
- bool
true
if the procedure address was found; otherwise,false
.
Write<T>(IntPtr, T)
Writes a value to the specified unmanaged memory location.
public static void Write<T>(this IntPtr ptr, T value) where T : struct
Parameters
ptr
IntPtrThe address in unmanaged memory to write to.
value
TThe value to write.
Type Parameters
T
The type of the value to write. Must be a supported primitive type (e.g., byte, short, int, long, IntPtr).
Exceptions
- ArgumentException
Thrown when
T
is not a supported type.