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 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.
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.
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.
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.
public static bool FreeLibrary(IntPtr hModule)
result = Marshaler.FreeLibrary(hModule)
Frees a previously loaded unmanaged library.
- hModule
- The handle to the library to free.
戻り値: true if the library was successfully freed; otherwise, false.
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.
戻り値: A pointer to the unmanaged ANSI string.
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.
戻り値: A pointer to the unmanaged string.
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.
戻り値: A pointer to the unmanaged BSTR.
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.
戻り値: A pointer to the unmanaged Unicode string.
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.
戻り値: A delegate of type that wraps the unmanaged function.
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.
戻り値: A delegate of type for the specified procedure.
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.
戻り値: The address of the procedure in unmanaged memory.
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.
戻り値: The decoded managed string, or null if the source is zero.
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).
戻り値: A handle to the loaded library.
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.
戻り値: The value read from the unmanaged memory, cast to type .
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.
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.
戻り値: A tuple containing the pointer to the unmanaged memory and its size in bytes.
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.
戻り値: A pointer to the allocated unmanaged memory containing the marshaled data.
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.
戻り値: The managed string representation of the ANSI string.
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.
戻り値: The managed string representation of the ANSI string.
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.
戻り値: A managed string representation of the UTF8 encoded string.
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.
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.
戻り値: The managed string representation.
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.
戻り値: The managed string representation.
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.
戻り値: The managed string representation of the BSTR.
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.
戻り値: A HGlobalSafeHandle wrapping the allocated unmanaged memory.
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.
戻り値: A HGlobalSafeHandle wrapping the allocated unmanaged memory.
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.
戻り値: A HGlobalSafeHandle wrapping the allocated unmanaged memory containing the encoded string.
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.
戻り値: A ReadOnlySpan representing the unmanaged memory.
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.
戻り値: A Span representing the unmanaged memory.
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.
戻り値: The decoded managed string, or null if is zero.
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.
戻り値: A managed object of type containing the data from the unmanaged memory.
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.
戻り値: The managed string representation of the Unicode string.
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.
戻り値: The managed string representation of the Unicode string.
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.
戻り値: A managed string representation of the UTF8 encoded string.
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.
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.
戻り値: A delegate of type if found; otherwise, null.
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.
戻り値: true if the procedure address was found; otherwise, false.
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.