Table of Contents

Class Marshaler

Namespace
Ecng.Interop
Assembly
Ecng.Interop.dll

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 IntPtr

The 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 IntPtr

The pointer to the unmanaged memory to copy from.

buffer byte[]

The byte array to copy the data into.

offset int

The starting index in the buffer where data should be copied.

length int

The 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 IntPtr

The pointer to the unmanaged memory to free.

FreeLibrary(IntPtr)

Frees a previously loaded unmanaged library.

public static bool FreeLibrary(this IntPtr hModule)

Parameters

hModule IntPtr

The 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 string

The 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 string

The 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 string

The 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 string

The 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 IntPtr

The 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 IntPtr

The handle to the loaded unmanaged library.

procName string

The 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 IntPtr

The handle to the loaded unmanaged library.

procName string

The 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 Encoding

The encoding to use for decoding the string.

srcChar byte

A reference to the starting byte in unmanaged memory.

maxBytes int

The 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 string

The 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 IntPtr

The 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 Encoding

The encoding to use for encoding the string.

tgtChar byte

A reference to the target byte in unmanaged memory.

maxBytes int

The maximum number of bytes to write.

value string

The string to write.

Exceptions

ArgumentNullException

Thrown when encoding is null.

ArgumentOutOfRangeException

Thrown when maxBytes is negative or the string length exceeds maxBytes.

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 T

The 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 T

The 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 IntPtr

The 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 IntPtr

The pointer to the ANSI string in unmanaged memory.

len int

The 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 IntPtr

The 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

ptr IntPtr

The pointer to the string in unmanaged memory.

len int

The length of the string to read.

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 IntPtr

The 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 int

The 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

ptr IntPtr

The size of the memory to allocate, as an IntPtr.

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 Encoding

The encoding to use for the string.

data string

The 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 Encoding

The encoding to use for decoding the string.

pData IntPtr

The 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 IntPtr

The 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 IntPtr

The 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 IntPtr

The pointer to the Unicode string in unmanaged memory.

len int

The 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 IntPtr

The handle to the loaded unmanaged library.

procName string

The 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 IntPtr

The handle to the loaded unmanaged library.

procName string

The name of the procedure to retrieve.

address IntPtr

When 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 IntPtr

The address in unmanaged memory to write to.

value T

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