Marshal.StructureToPtr<T> Method (T, IntPtr, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

[Supported in the .NET Framework 4.5.1 and later versions]

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

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

public static void StructureToPtr<T>(
	T structure,
	IntPtr ptr,
	bool fDeleteOld


Type: T

A managed object that holds the data to be marshaled. The object must be a structure or an instance of a formatted class.

Type: System.IntPtr

A pointer to an unmanaged block of memory, which must be allocated before this method is called.

Type: System.Boolean

true to call the DestroyStructure<T>(IntPtr) method on the ptr parameter before this method copies the data. The block must contain valid data. Note that passing false when the memory block already contains data can lead to a memory leak.

Type Parameters


The type of the managed object.

Exception Condition

structure is a reference type that is not a formatted class.

A formatted class is a reference type whose layout is specified by the StructLayoutAttribute attribute, as either LayoutKind.Explicit or LayoutKind.Sequential.

StructureToPtr<T>(T, IntPtr, Boolean) 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 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 DestroyStructure<T>(IntPtr) method before you free the memory block.

If you use theStructureToPtr<T>(T, IntPtr, Boolean) method 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 types and unmanaged copies are effectively leaked.

The overall pattern for usingStructureToPtr<T>(T, IntPtr, Boolean) is as follows:

  1. On the first call to theStructureToPtr<T> method after a memory block has been allocated, fDeleteOld must be false, because there are no contents to clear.


    Specify true for fDeleteOld only if the block contains valid data.

  2. 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.

  3. If the object contains reference types, you must call the DestroyStructure<T> method before you free the memory block.


To pin an existing structure instead of copying it, use the System.Runtime.InteropServices.GCHandle type to create a pinned handle for the structure. For details on how to pin, see Copying and Pinning.


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
Available since 8.1
.NET Framework
Available since 4.5.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top