Export (0) Print
Expand All

Marshal.StructureToPtr Method (Object, IntPtr, Boolean)

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

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

[ComVisibleAttribute(true)]
public static void StructureToPtr(
	Object structure,
	IntPtr ptr,
	bool fDeleteOld
)

Parameters

structure
Type: System.Object

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

ptr
Type: System.IntPtr

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

fDeleteOld
Type: System.Boolean

true to call the Marshal.DestroyStructure 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.

ExceptionCondition
ArgumentException

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

-or-

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.

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

StructureToPtr 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 Marshal.DestroyStructure method before you free the memory block.

If you use the StructureToPtr 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 using StructureToPtr is as follows:

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

    Important noteImportant

    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 method before you free the memory block.

NoteNote

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.

The following example creates a managed structure, transfers it to unmanaged memory using the StructureToPtr method, and then transfers it back to managed memory using the PtrToStructure method.

using System;
using System.Runtime.InteropServices;

public struct Point
{
    public int x;
    public int y;
}

class Example
{

    static void Main()
    {

        // Create a point struct.
        Point p;
        p.x = 1;
        p.y = 1;

        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");

        // Initialize unmanged memory to hold the struct.
        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));

        try
        {

            // Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, false);

            // Create another point.
            Point anotherP;

            // Set this Point to the value of the  
            // Point in unmanaged memory. 
            anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));

            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");

        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }



    }

}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

  • SecurityCriticalAttribute 

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft