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
ptrIntPtrThe pointer to the unmanaged memory to copy from.
bufferbyte[]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
ptrIntPtrThe pointer to the unmanaged memory to copy from.
bufferbyte[]The byte array to copy the data into.
offsetintThe starting index in the buffer where data should be copied.
lengthintThe 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
ptrIntPtrThe pointer to the unmanaged memory to free.
FreeLibrary(IntPtr)
Frees a previously loaded unmanaged library.
public static bool FreeLibrary(this IntPtr hModule)
Parameters
hModuleIntPtrThe handle to the library to free.
Returns
- bool
trueif 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
strstringThe 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
strstringThe 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
strstringThe 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
strstringThe 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
ptrIntPtrThe unmanaged function pointer to convert.
Returns
- T
A delegate of type
Tthat wraps the unmanaged function.
Type Parameters
TThe 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
libraryIntPtrThe handle to the loaded unmanaged library.
procNamestringThe name of the procedure to retrieve.
Returns
- T
A delegate of type
Tfor the specified procedure.
Type Parameters
TThe 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
hModuleIntPtrThe handle to the loaded unmanaged library.
procNamestringThe 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
encodingEncodingThe encoding to use for decoding the string.
srcCharbyteA reference to the starting byte in unmanaged memory.
maxBytesintThe maximum number of bytes to read.
Returns
- string
The decoded managed string, or
nullif the source is zero.
Exceptions
- ArgumentNullException
Thrown when
encodingis null.- ArgumentOutOfRangeException
Thrown when
maxBytesis negative.
LoadLibrary(string)
Loads an unmanaged library from the specified path.
public static IntPtr LoadLibrary(string dllPath)
Parameters
dllPathstringThe file path to the unmanaged library (DLL).
Returns
- IntPtr
A handle to the loaded library.
Exceptions
- ArgumentNullException
Thrown when
dllPathis 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
ptrIntPtrThe address in unmanaged memory to read from.
Returns
- T
The value read from the unmanaged memory, cast to type
T.
Type Parameters
TThe type of the value to read. Must be a supported primitive type (e.g., byte, short, int, long, IntPtr).
Exceptions
- ArgumentException
Thrown when
Tis 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
encodingEncodingThe encoding to use for encoding the string.
tgtCharbyteA reference to the target byte in unmanaged memory.
maxBytesintThe maximum number of bytes to write.
valuestringThe string to write.
Exceptions
- ArgumentNullException
Thrown when
encodingis null.- ArgumentOutOfRangeException
Thrown when
maxBytesis 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
structureTThe managed object to marshal.
sizeint?The optional size of the unmanaged memory block. If null, the size of
Tis used.
Returns
- (IntPtr ptr, int size)
A tuple containing the pointer to the unmanaged memory and its size in bytes.
Type Parameters
TThe 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
structureTThe managed object to marshal.
sizeint?The optional size of the unmanaged memory block. If null, the size of
Tis used.
Returns
- IntPtr
A pointer to the allocated unmanaged memory containing the marshaled data.
Type Parameters
TThe 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
ptrIntPtrThe 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
ptrIntPtrThe pointer to the ANSI string in unmanaged memory.
lenintThe 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
ptrIntPtrThe 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
ptrIntPtrThe 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
ptrintThe 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
encodingEncodingThe encoding to use for the string.
datastringThe string to encode and allocate.
Returns
- HGlobalSafeHandle
A HGlobalSafeHandle wrapping the allocated unmanaged memory containing the encoded string.
Exceptions
- ArgumentNullException
Thrown when
encodingis 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
encodingEncodingThe encoding to use for decoding the string.
pDataIntPtrThe pointer to the ANSI string in unmanaged memory.
Returns
- string
The decoded managed string.
Exceptions
- ArgumentNullException
Thrown when
encodingis 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
ptrIntPtrThe pointer to the unmanaged block of memory.
Returns
- T
A managed object of type
Tcontaining the data from the unmanaged memory.
Type Parameters
TThe 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
ptrIntPtrThe 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
ptrIntPtrThe pointer to the Unicode string in unmanaged memory.
lenintThe 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
libraryIntPtrThe handle to the loaded unmanaged library.
procNamestringThe name of the procedure to retrieve.
Returns
- T
A delegate of type
Tif found; otherwise,null.
Type Parameters
TThe 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
hModuleIntPtrThe handle to the loaded unmanaged library.
procNamestringThe name of the procedure to retrieve.
addressIntPtrWhen this method returns, contains the address of the procedure if found; otherwise, Zero.
Returns
- bool
trueif 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
ptrIntPtrThe address in unmanaged memory to write to.
valueTThe value to write.
Type Parameters
TThe type of the value to write. Must be a supported primitive type (e.g., byte, short, int, long, IntPtr).
Exceptions
- ArgumentException
Thrown when
Tis not a supported type.