Marshal.StructureToPtr Method (Object, IntPtr, Boolean)
Marshals data from a managed object to an unmanaged block of memory.
Assembly: mscorlib (in mscorlib.dll)
<SecurityCriticalAttribute> <ComVisibleAttribute(True)> Public Shared Sub StructureToPtr ( structure As Object, ptr As IntPtr, fDeleteOld As Boolean )
A managed object that holds the data to be marshaled. This object must be a structure or an instance of a formatted class.
A pointer to an unmanaged block of memory, which must be allocated before this method is called.
structure is a reference type that is not a formatted class.
structure is a generic type.
If structure is a value type, it can be boxed or unboxed. If it is boxed, it is unboxed before copying.
System.Object), the managed objects are kept alive with reference counts. All other reference types (for example, strings and arrays) are marshaled to copies. To release these managed or unmanaged objects, you must call the Marshal.DestroyStructuremethod before you free the memory block.copies the contents of structure to the pre-allocated block of memory that the ptr parameter points to. If structure contains reference types that marshal to COM interface pointers (interfaces, classes without layout, and
If you use themethod to copy a different instance to the memory block at a later time, specify true for fDeleteOld to remove reference counts for reference types in the previous instance. Otherwise, the managed reference typesand unmanaged copies are effectively leaked.
The overall pattern for usingis as follows:
On the first call to themethod after a memory block has been allocated, fDeleteOldmust befalse, because there are no contents to clear.
Specify true for fDeleteOld only if the block contains valid data.
If you copy a different instance to the memory block, and the object contains reference types, fDeleteOld must be true to free reference types in the old contents.
If the object contains reference types, you must call the DestroyStructure method before you free the memory block.
The following example creates a managed structure, transfers it to unmanaged memory using the PtrToStructure method.method, and then transfers it back to managed memory using the
Imports System Imports System.Runtime.InteropServices Public Structure Point Public x As Integer Public y As Integer End Structure Module Example Sub Main() ' Create a point struct. Dim p As Point p.x = 1 p.y = 1 Console.WriteLine("The value of first point is " + p.x.ToString + " and " + p.y.ToString + ".") ' Initialize unmanged memory to hold the struct. Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p)) Try ' Copy the struct to unmanaged memory. Marshal.StructureToPtr(p, pnt, False) ' Create another point. Dim anotherP As Point ' Set this Point to the value of the ' Point in unmanaged memory. anotherP = CType(Marshal.PtrToStructure(pnt, GetType(Point)), Point) Console.WriteLine("The value of new point is " + anotherP.x.ToString + " and " + anotherP.y.ToString + ".") Finally ' Free the unmanaged memory. Marshal.FreeHGlobal(pnt) End Try End Sub End Module
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Available since 8
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1