public sealed class Marshal
|
For example, the System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi (not supported on the shared source CLI) method copies the ANSI characters from a specified string (in managed heap) to a buffer in unmanaged heap. It also allocates the target heap of the right size. For example:
String s = "Hello"; IntPtr p = Marshal.StringToHGlobalAnsi(s);
SystemDefaultCharSize | Represents the default character size on the system. The default is 2 for Unicode systems and 1 for ANSI systems. |
SystemMaxDBCSCharSize | The maximum size of a DBCS character. |
AllocHGlobal | Overloaded:AllocHGlobal(int cb) Allocates memory from the unmanaged native heap of the process using GlobalAlloc. |
AllocHGlobal | Overloaded:AllocHGlobal(IntPtr cb) Allocates memory from the unmanaged native heap of the process using GlobalAlloc. |
Copy | Overloaded:Copy(byte[] source, int startIndex, IntPtr destination, int length) Copies data from a managed byte array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(char[] source, int startIndex, IntPtr destination, int length) Copies data from a managed char array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(double[] source, int startIndex, IntPtr destination, int length) Copies data from a managed double array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(short[] source, int startIndex, IntPtr destination, int length) Copies data from a managed short array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(int[] source, int startIndex, IntPtr destination, int length) Copies data from a managed int array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(long[] source, int startIndex, IntPtr destination, int length) Copies data from a managed long array to an unmanaged memory pointer. |
Copy | Overloaded:Copy(IntPtr source, byte[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed byte array. |
Copy | Overloaded:Copy(IntPtr source, char[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed char array. |
Copy | Overloaded:Copy(IntPtr source, double[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed double array. |
Copy | Overloaded:Copy(IntPtr source, short[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed short array. |
Copy | Overloaded:Copy(IntPtr source, int[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed int array. |
Copy | Overloaded:Copy(IntPtr source, long[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed long array. |
Copy | Overloaded:Copy(IntPtr source, float[] destination, int startIndex, int length) Copies data from an unmanaged memory pointer to a managed float array. |
Copy | Overloaded:Copy(float[] source, int startIndex, IntPtr destination, int length) Copies data from a managed float array to an unmanaged memory pointer. |
DestroyStructure | Frees all substructures pointed to by the specified native memory block. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
FreeHGlobal | Frees memory previously allocated from the unmanaged native heap of the process with Marshal.AllocHGlobal. |
GetExceptionCode | Retrieves a code that identifies the type of the exception that occurred. |
GetExceptionPointers | Retrieves a machine independent description of an exception, and information about the machine state that existed for the thread when the exception occurred. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetHINSTANCE | Returns the HInstance for the specified module. |
GetHRForException | Converts the specified exception to an HRESULT. |
GetHRForLastWin32Error | Returns the HRESULT corresponding to the last error incurred by Win32 code executed using Marshal. |
GetLastWin32Error | Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
NumParamBytes | Calculates the number of bytes required to hold the parameters for the specified method. |
OffsetOf | Returns the field offset of the unmanaged form of the class. |
Prelink | Executes the one-time method set up tasks without calling the method. |
PrelinkAll | Performs a pre-link check for all methods on a class. |
PtrToStringAnsi | Overloaded:PtrToStringAnsi(IntPtr ptr) Copies an ANSI string to a managed String object. Widens each ANSI character to Unicode. |
PtrToStringAnsi | Overloaded:PtrToStringAnsi(IntPtr ptr, int len) Copies a specified number of characters from an ANSI string to a managed String object. Widens each ANSI character to Unicode if needed. |
PtrToStringAuto | Overloaded:PtrToStringAuto(IntPtr ptr) Copies a string stored in native heap to a managed String object. |
PtrToStringAuto | Overloaded:PtrToStringAuto(IntPtr ptr, int len) Copies a specified number of characters from a string stored in native heap to a managed String object. |
PtrToStringUni | Overloaded:PtrToStringUni(IntPtr ptr) Copies a Unicode string stored in native heap to a managed String object. |
PtrToStringUni | Overloaded:PtrToStringUni(IntPtr ptr, int len) Copies a specified number of characters from a Unicode string stored in native heap to a managed String object. |
PtrToStructure | Overloaded:PtrToStructure(IntPtr ptr, object structure) Marshals data from an unmanaged block of memory pointed to a managed object. |
PtrToStructure | Overloaded:PtrToStructure(IntPtr ptr, Type structureType) Marshals data from an unmanaged block of memory to a newly allocated object of the specified type. |
ReadByte | Overloaded:ReadByte(IntPtr ptr) Reads a single byte from an unmanaged pointer. |
ReadByte | Overloaded:ReadByte(IntPtr ptr, int ofs) Reads a single byte from an unmanaged pointer. |
ReadByte | Overloaded:ReadByte(in object ptr, int ofs) Reads a single byte from an unmanaged pointer. |
ReadInt16 | Overloaded:ReadInt16(IntPtr ptr) Reads a 16-bit integer from native heap. |
ReadInt16 | Overloaded:ReadInt16(IntPtr ptr, int ofs) Reads a 16-bit integer from native heap. |
ReadInt16 | Overloaded:ReadInt16(in object ptr, int ofs) Reads a 16-bit integer from native heap. |
ReadInt32 | Overloaded:ReadInt32(IntPtr ptr) Reads a 32-bit integer from native heap. |
ReadInt32 | Overloaded:ReadInt32(IntPtr ptr, int ofs) Reads a 32-bit integer from native heap. |
ReadInt32 | Overloaded:ReadInt32(in object ptr, int ofs) Reads a 32-bit integer from native heap. |
ReadInt64 | Overloaded:ReadInt64(IntPtr ptr) Reads a 64-bit integer from native heap. |
ReadInt64 | Overloaded:ReadInt64(IntPtr ptr, int ofs) Reads a 64-bit integer from native heap. |
ReadInt64 | Overloaded:ReadInt64(in object ptr, int ofs) Reads a 64-bit integer from native heap. |
ReadIntPtr | Overloaded:ReadIntPtr(IntPtr ptr) Reads a processor native sized integer from native heap. |
ReadIntPtr | Overloaded:ReadIntPtr(IntPtr ptr, int ofs) Reads a processor native sized integer from native heap. |
ReadIntPtr | Overloaded:ReadIntPtr(in object ptr, int ofs) Reads a processor native sized integer from native heap. |
SizeOf | Overloaded:SizeOf(object structure) Returns the unmanaged size of an object in bytes. |
SizeOf | Overloaded:SizeOf(Type t) Returns the unmanaged size of a type in bytes. |
StructureToPtr | Marshals data from a managed object to an unmanaged block of memory. |
ThrowExceptionForHR | Overloaded:ThrowExceptionForHR(int errorCode) Throws an exception with a specific HRESULT value. |
ThrowExceptionForHR | Overloaded:ThrowExceptionForHR(int errorCode, IntPtr errorInfo) Throws an exception with a specific HRESULT. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
UnsafeAddrOfPinnedArrayElement | Gets the address of the element at the specified index inside the specified array. |
WriteByte | Overloaded:WriteByte(IntPtr ptr, byte val) Writes a single byte value into native heap. |
WriteByte | Overloaded:WriteByte(IntPtr ptr, int ofs, byte val) Writes a single byte value into native heap. |
WriteByte | Overloaded:WriteByte(in object ptr, int ofs, byte val) Writes a single byte value into native heap. |
WriteInt16 | Overloaded:WriteInt16(IntPtr ptr, char val) Writes a 16-bit integer value into native heap. |
WriteInt16 | Overloaded:WriteInt16(IntPtr ptr, short val) Writes a 16-bit integer value into native heap. |
WriteInt16 | Overloaded:WriteInt16(IntPtr ptr, int ofs, char val) Writes a 16-bit integer value into native heap. |
WriteInt16 | Overloaded:WriteInt16(IntPtr ptr, int ofs, short val) Writes a 16-bit integer value into native heap. |
WriteInt16 | Overloaded:WriteInt16(in object ptr, int ofs, char val) Writes a 16-bit integer value into native heap. |
WriteInt16 | Overloaded:WriteInt16(in object ptr, int ofs, short val) Writes a 16-bit integer value into native heap. |
WriteInt32 | Overloaded:WriteInt32(IntPtr ptr, int val) Writes a 32-bit integer value into native heap. |
WriteInt32 | Overloaded:WriteInt32(IntPtr ptr, int ofs, int val) Writes a 32-bit integer value into native heap. |
WriteInt32 | Overloaded:WriteInt32(in object ptr, int ofs, int val) Writes a 32-bit integer value into native heap. |
WriteInt64 | Overloaded:WriteInt64(IntPtr ptr, long val) Writes a 64-bit integer value into native heap. |
WriteInt64 | Overloaded:WriteInt64(IntPtr ptr, int ofs, long val) Writes a 64-bit integer value into native heap. |
WriteInt64 | Overloaded:WriteInt64(in object ptr, int ofs, long val) Writes a 64-bit integer value into native heap. |
WriteIntPtr | Overloaded:WriteIntPtr(IntPtr ptr, IntPtr val) Writes a processor native sized integer value into native heap. |
WriteIntPtr | Overloaded:WriteIntPtr(IntPtr ptr, int ofs, IntPtr val) Writes a processor native sized integer value into native heap. |
WriteIntPtr | Overloaded:WriteIntPtr(in object ptr, int ofs, IntPtr val) Writes a processor native sized integer value into native heap. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public static readonly int SystemDefaultCharSize;
|
public static readonly int SystemMaxDBCSCharSize;
|
cb
Exception Type | Condition |
---|---|
OutOfMemoryException | There is insufficient memory to satisfy the request. |
cb
Exception Type | Condition |
---|---|
OutOfMemoryException | There is insufficient memory to satisfy the request. |
source
startIndex
destination
length
source
startIndex
destination
length
public static void Copy( |
source
startIndex
destination
length
public static void Copy( |
source
startIndex
destination
length
source
startIndex
destination
length
source
startIndex
destination
length
source
destination
startIndex
length
source
destination
startIndex
length
public static void Copy( |
source
destination
startIndex
length
public static void Copy( |
source
destination
startIndex
length
source
destination
startIndex
length
source
destination
startIndex
length
public static void Copy( |
source
destination
startIndex
length
public static void Copy( |
source
startIndex
destination
length
ptr
structuretype
~Marshal(); |
public static void FreeHGlobal( |
hglobal
Exception Type | Condition |
---|---|
ArgumentException | There is any failure. |
public static int GetExceptionCode(); |
public static IntPtr GetExceptionPointers(); |
public virtual int GetHashCode(); |
m
e
public static int GetHRForLastWin32Error(); |
public static int GetLastWin32Error(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public static int NumParamBytes( |
m
t
fieldName
Exception Type | Condition |
---|---|
ArgumentException | The class cannot be exported as a structure. |
public static void Prelink( |
m
First-time tasks include checking to ensure that the platform invoke metadata is correctly formatted, that all the managed types are valid to have in a platform invoke function, locating and loading the unmanaged DLL into the process, and locating the entrypoint in the process.
public static void PrelinkAll( |
c
ptr
Exception Type | Condition |
---|---|
ArgumentException | ptr is null. |
ptr
len
Exception Type | Condition |
---|---|
ArgumentException | ptr is null. -or- len is less than zero. |
ptr
ptr
len
ptr
ptr
len
ptr
structure
ptr
structureType
ptr
ptr
ofs
ptr
ofs
ptr
ptr
ofs
ptr
ofs
ptr
ptr
ofs
ptr
ofs
ptr
ptr
ofs
ptr
ofs
ptr
ptr
ofs
ptr
ofs
structure
Exception Type | Condition |
---|---|
ArgumentException | structure is not a Marshal class. -or- structure is an array type. |
NullReferenceException | structure is null. |
t
Exception Type | Condition |
---|---|
ArgumentException | structure is not a Marshal class. -or- structure is an array type. |
NullReferenceException | structure is null. |
structure
ptr
fDeleteOld
Suppose your unmanaged block of memory is pointed to by ptr. The layout of this block is described by a corresponding managed class structure.StructureToPtr marshals field values from a structure to a pointer. Suppose the ptr block includes a reference field, pointing to a string buffer currently holding "abc". Suppose the corresponding field on the managed side is a string holding "vwxyz". If you do not tell it otherwise, StructureToPtr allocates a new unmanaged buffer to hold "vwxyz", and hooks it up to the ptr block. This will cast the old buffer "abc" adrift without freeing it back to the unmanaged heap. You end up with an orphan buffer that represents a memory leak in your code. If you set the fDeleteOld bit true, StructureToPtr frees the buffer holding "abc" before going on to allocate a new buffer for "vwxyz".
public static void ThrowExceptionForHR( |
errorCode
For more information, please see the existing documentation for GetErrorInfo in the MSDN library.
errorCode
errorInfo
public virtual string ToString(); |
arr
index
ptr
val
ptr
ofs
val
ptr
ofs
val
ptr
val
ptr
val
ptr
ofs
val
ptr
ofs
val
ptr
ofs
val
ptr
ofs
val
ptr
val
ptr
ofs
val
ptr
ofs
val
ptr
val
ptr
ofs
val
ptr
ofs
val
ptr
val
ptr
ofs
val
ptr
ofs
val