Extra methods on top of System.Runtime.InteropServices.Marshal for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types
MarshalExtra
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| AllocHGlobal | 1 | Allocates memory from the unmanaged memory of the process for a given type |
| PtrToStructure | 1 | Marshals data from an unmanaged block of memory to a newly allocated managed object of the type specified by a generic type parameter. |
| StructureToPtr | 1 | Marshals data from a managed object of a specified type to an unmanaged block of memory. |
| FreeNativeStruct | 1 | Frees all substructures of a specified type that the specified unmanaged memory block points to. |
03 Members
Allocates memory from the unmanaged memory of the process for a given type
| Name | Description |
|---|---|
T | A type that has an equivalent in the unmanaged world e.g. int or a struct Point |
A pointer to the newly allocated memory. This memory must be released using the FreeHGlobal(IntPtr) method, or related.
IntPtr, Boolean)Marshals data from an unmanaged block of memory to a newly allocated managed object of the type specified by a generic type parameter. Note it is almost superseded in .NET Framework 4.5.1 and later versions; consider your needs
| Name | Description |
|---|---|
T | The type of the object to which the data is to be copied. This must be a structure. |
| Name | Type | Description |
|---|---|---|
ptr | IntPtr | A pointer to an unmanaged block of memory. |
cleanup | Boolean | (Optional) If true, free the native memory block pointed to by ptr. This feature is handy in generated marshalling code. |
A managed object that contains the data that the ptr parameter points to
``0)Marshals data from a managed object of a specified type to an unmanaged block of memory. Note it is almost superseded in .NET Framework 4.5.1 and later versions; consider your needs
| Name | Description |
|---|---|
T | The type of the managed object. |
| Name | Type | Description |
|---|---|---|
structure | ``0 | A managed object that holds the data to be marshaled. The object must be a structure. |
A pointer to a newly allocated unmanaged block of memory.
IntPtr, ``0, Boolean)Frees all substructures of a specified type that the specified unmanaged memory block points to.
| Name | Description |
|---|---|
T | The type of the managed object. |
| Name | Type | Description |
|---|---|---|
ptr | IntPtr | A pointer to an unmanaged block of memory. |
managedObject | ``0 | [in,out] The managed object. |
copy | Boolean | (Optional) True to copy. |