Marshaler

Ecng.Interop

Provides a collection of extended methods that manipulate and extend the functionality of the Marshal class for interoperating with unmanaged memory and libraries.

Methods

CopyTo
public static void CopyTo(IntPtr ptr, byte[] buffer)
Marshaler.CopyTo(ptr, buffer)

Copies data from an unmanaged pointer to a byte array.

ptr
The pointer to the unmanaged memory to copy from.
buffer
The byte array to copy the data into.
CopyTo
public static void CopyTo(IntPtr ptr, byte[] buffer, int offset, int length)
Marshaler.CopyTo(ptr, buffer, offset, length)

Copies a specified amount of data from an unmanaged pointer to a byte array.

ptr
The pointer to the unmanaged memory to copy from.
buffer
The byte array to copy the data into.
offset
The starting index in the buffer where data should be copied.
length
The number of bytes to copy.
FillString
public static void FillString(string value, Encoding encoding, Byte* ptr, int bytesCount)
Marshaler.FillString(value, encoding, ptr, bytesCount)

Copies the contents of the string into unmanaged memory using the specified encoding.

value
The managed string to copy.
encoding
The encoding to use.
ptr
A pointer to the target unmanaged memory.
bytesCount
The maximum size of the unmanaged memory block in bytes.
FreeHGlobal
public static void FreeHGlobal(IntPtr ptr)
Marshaler.FreeHGlobal(ptr)

Frees an unmanaged memory block previously allocated with IntPtr).

ptr
The pointer to the unmanaged memory to free.
FreeLibrary
public static bool FreeLibrary(IntPtr hModule)
result = Marshaler.FreeLibrary(hModule)

Frees a previously loaded unmanaged library.

hModule
The handle to the library to free.

Returns: true if the library was successfully freed; otherwise, false.

FromAnsi
public static IntPtr FromAnsi(string str)
result = Marshaler.FromAnsi(str)

Converts a managed string to an unmanaged ANSI string and returns a pointer to it.

str
The managed string to convert.

Returns: A pointer to the unmanaged ANSI string.

FromAuto
public static IntPtr FromAuto(string str)
result = Marshaler.FromAuto(str)

Converts a managed string to an unmanaged string (platform-dependent encoding) and returns a pointer to it.

str
The managed string to convert.

Returns: A pointer to the unmanaged string.

FromBSTR
public static IntPtr FromBSTR(string str)
result = Marshaler.FromBSTR(str)

Converts a managed string to an unmanaged BSTR and returns a pointer to it.

str
The managed string to convert.

Returns: A pointer to the unmanaged BSTR.

FromUnicode
public static IntPtr FromUnicode(string str)
result = Marshaler.FromUnicode(str)

Converts a managed string to an unmanaged Unicode string and returns a pointer to it.

str
The managed string to convert.

Returns: A pointer to the unmanaged Unicode string.

GetDelegateForFunctionPointer``1
public static T GetDelegateForFunctionPointer<T>(IntPtr ptr)
result = Marshaler.GetDelegateForFunctionPointer(ptr)

Converts an unmanaged function pointer to a delegate of the specified type.

ptr
The unmanaged function pointer to convert.

Returns: A delegate of type that wraps the unmanaged function.

GetHandler``1
public static T GetHandler<T>(IntPtr library, string procName)
result = Marshaler.GetHandler(library, procName)

Retrieves a delegate for a named procedure from an unmanaged library.

library
The handle to the loaded unmanaged library.
procName
The name of the procedure to retrieve.

Returns: A delegate of type for the specified procedure.

GetProcAddress
public static IntPtr GetProcAddress(IntPtr hModule, string procName)
result = Marshaler.GetProcAddress(hModule, procName)

Retrieves the address of a named procedure from an unmanaged library.

hModule
The handle to the loaded unmanaged library.
procName
The name of the procedure to retrieve.

Returns: The address of the procedure in unmanaged memory.

GetUnsafeString
public static string GetUnsafeString(Encoding encoding, byte srcChar, int maxBytes)
result = Marshaler.GetUnsafeString(encoding, srcChar, maxBytes)

Converts an unmanaged byte reference to a managed string using the specified encoding, with a maximum byte length.

encoding
The encoding to use for decoding the string.
srcChar
A reference to the starting byte in unmanaged memory.
maxBytes
The maximum number of bytes to read.

Returns: The decoded managed string, or null if the source is zero.

LoadLibrary
public static IntPtr LoadLibrary(string dllPath)
result = Marshaler.LoadLibrary(dllPath)

Loads an unmanaged library from the specified path.

dllPath
The file path to the unmanaged library (DLL).

Returns: A handle to the loaded library.

Read``1
public static T Read<T>(IntPtr ptr)
result = Marshaler.Read(ptr)

Reads a value from the specified unmanaged memory location.

ptr
The address in unmanaged memory to read from.

Returns: The value read from the unmanaged memory, cast to type .

SetUnsafeString
public static void SetUnsafeString(Encoding encoding, byte tgtChar, int maxBytes, string value)
Marshaler.SetUnsafeString(encoding, tgtChar, maxBytes, value)

Writes a managed string to an unmanaged byte reference using the specified encoding, with a maximum byte length.

encoding
The encoding to use for encoding the string.
tgtChar
A reference to the target byte in unmanaged memory.
maxBytes
The maximum number of bytes to write.
value
The string to write.
StructToPtrEx``1
public static ValueTuple<IntPtr, int> StructToPtrEx<T>(T structure, int? size)
result = Marshaler.StructToPtrEx(structure, size)

Marshals data from a managed object to an unmanaged block of memory and returns the pointer along with the size.

structure
The managed object to marshal.
size
The optional size of the unmanaged memory block. If null, the size of is used.

Returns: A tuple containing the pointer to the unmanaged memory and its size in bytes.

StructToPtr``1
public static IntPtr StructToPtr<T>(T structure, int? size)
result = Marshaler.StructToPtr(structure, size)

Marshals data from a managed object to an unmanaged block of memory and returns the pointer.

structure
The managed object to marshal.
size
The optional size of the unmanaged memory block. If null, the size of is used.

Returns: A pointer to the allocated unmanaged memory containing the marshaled data.

ToAnsi
public static string ToAnsi(IntPtr ptr, int len)
result = Marshaler.ToAnsi(ptr, len)

Converts an unmanaged ANSI string pointer to a managed string with a specified length.

ptr
The pointer to the ANSI string in unmanaged memory.
len
The length of the string to read.

Returns: The managed string representation of the ANSI string.

ToAnsi
public static string ToAnsi(IntPtr ptr)
result = Marshaler.ToAnsi(ptr)

Converts an unmanaged ANSI string pointer to a managed string.

ptr
The pointer to the ANSI string in unmanaged memory.

Returns: The managed string representation of the ANSI string.

ToAscii
public static string ToAscii(int size, Byte* ptr)
result = Marshaler.ToAscii(size, ptr)

Converts a pointer to a ASCII encoded string of a specified size into a managed string.

size
The size of the ASCII encoded string in bytes.
ptr
A pointer to the ASCII encoded string in unmanaged memory.

Returns: A managed string representation of the UTF8 encoded string.

ToAscii
public static void ToAscii(string value, Byte* ptr, int size)
Marshaler.ToAscii(value, ptr, size)

Fills a ASCII encoded unmanaged memory block with the contents of a managed string.

value
The managed string to encode and copy into unmanaged memory.
ptr
A pointer to the unmanaged memory block to fill.
size
The maximum size of the unmanaged memory block in bytes.
ToAuto
public static string ToAuto(IntPtr ptr, int len)
result = Marshaler.ToAuto(ptr, len)

Converts an unmanaged string pointer (platform-dependent encoding) to a managed string with a specified length.

ptr
The pointer to the string in unmanaged memory.
len
The length of the string to read.

Returns: The managed string representation.

ToAuto
public static string ToAuto(IntPtr ptr)
result = Marshaler.ToAuto(ptr)

Converts an unmanaged string pointer (platform-dependent encoding) to a managed string.

ptr
The pointer to the string in unmanaged memory.

Returns: The managed string representation.

ToBSTR
public static string ToBSTR(IntPtr ptr)
result = Marshaler.ToBSTR(ptr)

Converts an unmanaged BSTR pointer to a managed string.

ptr
The pointer to the BSTR in unmanaged memory.

Returns: The managed string representation of the BSTR.

ToHGlobal
public static HGlobalSafeHandle ToHGlobal(int ptr)
result = Marshaler.ToHGlobal(ptr)

Allocates unmanaged memory of the specified size and wraps it in a safe handle.

ptr
The size of the memory to allocate, interpreted as an integer.

Returns: A HGlobalSafeHandle wrapping the allocated unmanaged memory.

ToHGlobal
public static HGlobalSafeHandle ToHGlobal(IntPtr ptr)
result = Marshaler.ToHGlobal(ptr)

Allocates unmanaged memory of the specified size and wraps it in a safe handle.

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

Returns: A HGlobalSafeHandle wrapping the allocated unmanaged memory.

ToHGlobal
public static HGlobalSafeHandle ToHGlobal(Encoding encoding, string data)
result = Marshaler.ToHGlobal(encoding, data)

Encodes a string using the specified encoding, allocates unmanaged memory for it, and returns a safe handle.

encoding
The encoding to use for the string.
data
The string to encode and allocate.

Returns: A HGlobalSafeHandle wrapping the allocated unmanaged memory containing the encoded string.

ToReadOnlySpan
public static ReadOnlySpan<byte> ToReadOnlySpan(IntPtr ptr, int length)
result = Marshaler.ToReadOnlySpan(ptr, length)

Creates a read-only span from the unmanaged memory pointer.

ptr
The unmanaged memory pointer.
length
The number of bytes in the span.

Returns: A ReadOnlySpan representing the unmanaged memory.

ToSpan
public static Span<byte> ToSpan(IntPtr ptr, int length)
result = Marshaler.ToSpan(ptr, length)

Creates a span from the unmanaged memory pointer.

ptr
The unmanaged memory pointer.
length
The number of bytes in the span.

Returns: A Span representing the unmanaged memory.

ToString
public static string ToString(Encoding encoding, IntPtr pData)
result = Marshaler.ToString(encoding, pData)

Decodes a NUL-terminated unmanaged string from a pointer into a managed string using the specified encoding.

encoding
The encoding to use for decoding the string.
pData
The pointer to the NUL-terminated string in unmanaged memory.

Returns: The decoded managed string, or null if is zero.

ToStruct``1
public static T ToStruct<T>(IntPtr ptr)
result = Marshaler.ToStruct(ptr)

Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.

ptr
The pointer to the unmanaged block of memory.

Returns: A managed object of type containing the data from the unmanaged memory.

ToUnicode
public static string ToUnicode(IntPtr ptr, int len)
result = Marshaler.ToUnicode(ptr, len)

Converts an unmanaged Unicode string pointer to a managed string with a specified length.

ptr
The pointer to the Unicode string in unmanaged memory.
len
The length of the string to read.

Returns: The managed string representation of the Unicode string.

ToUnicode
public static string ToUnicode(IntPtr ptr)
result = Marshaler.ToUnicode(ptr)

Converts an unmanaged Unicode string pointer to a managed string.

ptr
The pointer to the Unicode string in unmanaged memory.

Returns: The managed string representation of the Unicode string.

ToUtf8
public static string ToUtf8(int size, Byte* ptr)
result = Marshaler.ToUtf8(size, ptr)

Converts a pointer to a UTF8 encoded string of a specified size into a managed string.

size
The size of the UTF8 encoded string in bytes.
ptr
A pointer to the UTF8 encoded string in unmanaged memory.

Returns: A managed string representation of the UTF8 encoded string.

ToUtf8
public static void ToUtf8(string value, Byte* ptr, int size)
Marshaler.ToUtf8(value, ptr, size)

Fills a UTF8 encoded unmanaged memory block with the contents of a managed string.

value
The managed string to encode and copy into unmanaged memory.
ptr
A pointer to the unmanaged memory block to fill.
size
The maximum size of the unmanaged memory block in bytes.
TryGetHandler``1
public static T TryGetHandler<T>(IntPtr library, string procName)
result = Marshaler.TryGetHandler(library, procName)

Attempts to retrieve a delegate for a named procedure from an unmanaged library.

library
The handle to the loaded unmanaged library.
procName
The name of the procedure to retrieve.

Returns: A delegate of type if found; otherwise, null.

TryGetProcAddress
public static bool TryGetProcAddress(IntPtr hModule, string procName, IntPtr address)
result = Marshaler.TryGetProcAddress(hModule, procName, address)

Attempts to retrieve the address of a named procedure from an unmanaged library.

hModule
The handle to the loaded unmanaged library.
procName
The name of the procedure to retrieve.
address
When this method returns, contains the address of the procedure if found; otherwise, Zero.

Returns: true if the procedure address was found; otherwise, false.

Write``1
public static void Write<T>(IntPtr ptr, T value)
Marshaler.Write(ptr, value)

Writes a value to the specified unmanaged memory location.

ptr
The address in unmanaged memory to write to.
value
The value to write.