Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Marshal.StructureToPtr (Object, IntPtr, Boolean)

 

Effettua il marshalling di dati da un oggetto gestito a un blocco di memoria non gestito.

Spazio dei nomi:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

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

Parametri

structure
Type: System.Object

Oggetto gestito che contiene i dati di cui effettuare il marshalling. L'oggetto deve essere una struttura o un'istanza di una classe formattata.

ptr
Type: System.IntPtr

Puntatore a un blocco di memoria non gestito che deve essere allocato prima della chiamata al metodo.

fDeleteOld
Type: System.Boolean

true per chiamare il metodo Marshal.DestroyStructure sul parametro ptr prima che il metodo esegua la copia dei dati. Il blocco deve contenere dati validi. Si noti che passare il valore false quando il blocco di memoria contiene già dei dati può causare una perdita di memoria.

Exception Condition
ArgumentException

structure è un tipo di riferimento che non corrisponde a una classe formattata.

-oppure-

structure è un tipo generico.

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 T:System.Runtime.InteropServices.StructLayoutAttribute attribute, as either F:System.Runtime.InteropServices.LayoutKind.Explicit or F:System.Runtime.InteropServices.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.DestroyStructuremethod before you free the memory block.

If you use the M:System.Runtime.InteropServices.Marshal.StructureToPtr(System.Object,System.IntPtr,System.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 typesand unmanaged copies are effectively leaked.

The overall pattern for using M:System.Runtime.InteropServices.Marshal.StructureToPtr(System.Object,System.IntPtr,System.Boolean) is as follows:

  1. On the first call to the M:System.Runtime.InteropServices.Marshal.StructureToPtr(System.Object,System.IntPtr,System.Boolean)method after a memory block has been allocated, fDeleteOldmust befalse, because there are no contents to clear.

    System_CAPS_importantImportante

    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 M:System.Runtime.InteropServices.Marshal.DestroyStructure(System.IntPtr,System.Type) method before you free the memory block.

System_CAPS_noteNota

To pin an existing structure instead of copying it, use the T: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 M:System.Runtime.InteropServices.Marshal.StructureToPtr(System.Object,System.IntPtr,System.Boolean) method, and then transfers it back to managed memory using the M:System.Runtime.InteropServices.Marshal.PtrToStructure(System.IntPtr,System.Type) 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);
        }



    }

}

SecurityCriticalAttribute

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

Universal Windows Platform
Disponibile da 8
.NET Framework
Disponibile da 1.1
Libreria di classi portabile
Supportato in: piattaforme .NET portabili
Silverlight
Disponibile da 2.0
Windows Phone Silverlight
Disponibile da 7.0
Windows Phone
Disponibile da 8.1
Torna all'inizio
Mostra: