Freigeben über


Marshal.StructureToPtr-Methode: (Object, IntPtr, Boolean)

 

Marshallt Daten aus einem verwalteten Objekt zu einem nicht verwalteten Speicherblock.

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

Syntax

[SecurityCriticalAttribute]
[ComVisibleAttribute(true)]
public static void StructureToPtr(
    object structure,
    IntPtr ptr,
    bool fDeleteOld
)
public:
[SecurityCriticalAttribute]
[ComVisibleAttribute(true)]
static void StructureToPtr(
    Object^ structure,
    IntPtr ptr,
    bool fDeleteOld
)
[<SecurityCriticalAttribute>]
[<ComVisibleAttribute(true)>]
static member StructureToPtr : 
        structure:Object *
        ptr:nativeint *
        fDeleteOld:bool -> unit
<SecurityCriticalAttribute>
<ComVisibleAttribute(True)>
Public Shared Sub StructureToPtr (
    structure As Object,
    ptr As IntPtr,
    fDeleteOld As Boolean
)

Parameter

  • structure
    Type: System.Object

    Ein verwaltetes Objekt mit den zu marshallenden Daten. Dieses Objekt muss eine Struktur oder Instanz einer formatierten Klasse sein.

  • ptr
    Type: System.IntPtr

    Ein Zeiger auf einen nicht verwalteten Speicherblock, der vor dem Aufruf der Methode belegt werden muss.

  • fDeleteOld
    Type: System.Boolean

    true, wenn die Marshal.DestroyStructure-Methode für den ptr-Parameter aufgerufen werden soll, bevor diese Methode die Daten kopiert. Der Block muss gültige Daten enthalten. Beachten Sie, dass das Übergeben von false, wenn der Speicherblock bereits Daten enthält, zu einem Speicherverlust führen kann.

Ausnahmen

Exception Condition
ArgumentException

structure ist ein Verweistyp, der keine formatierte Klasse ist.

- oder -

structure ist ein generischer Typ.

Hinweise

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.

    Wichtig

    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.

Hinweis

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.

Beispiele

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);
        }



    }

}
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

Sicherheit

SecurityCriticalAttribute

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

Versionsinformationen

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1

Siehe auch

DestroyStructure
GCHandle
StructureToPtr Überladen
Marshal-Klasse
System.Runtime.InteropServices-Namespace

Zurück zum Anfang