Marshal.StructureToPtr 方法

定义

重载

StructureToPtr(Object, IntPtr, Boolean)
已过时.

将数据从托管对象封送到非托管内存块。

StructureToPtr<T>(T, IntPtr, Boolean)

将数据从指定类型的托管对象封送到非托管内存块。

StructureToPtr(Object, IntPtr, Boolean)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.CoreCLR.cs

注意

StructureToPtr(Object, IntPtr, Boolean) may be unavailable in future releases. Instead, use StructureToPtr<T>(T, IntPtr, Boolean). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296516

将数据从托管对象封送到非托管内存块。

public:
 static void StructureToPtr(System::Object ^ structure, IntPtr ptr, bool fDeleteOld);
[System.Obsolete("StructureToPtr(Object, IntPtr, Boolean) may be unavailable in future releases. Instead, use StructureToPtr<T>(T, IntPtr, Boolean). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296516")]
[System.Security.SecurityCritical]
public static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
public static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
[System.Security.SecurityCritical]
public static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
[System.Runtime.InteropServices.ComVisible(true)]
public static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(true)]
public static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
[<System.Obsolete("StructureToPtr(Object, IntPtr, Boolean) may be unavailable in future releases. Instead, use StructureToPtr<T>(T, IntPtr, Boolean). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296516")>]
[<System.Security.SecurityCritical>]
static member StructureToPtr : obj * nativeint * bool -> unit
static member StructureToPtr : obj * nativeint * bool -> unit
[<System.Security.SecurityCritical>]
static member StructureToPtr : obj * nativeint * bool -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
static member StructureToPtr : obj * nativeint * bool -> unit
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(true)>]
static member StructureToPtr : obj * nativeint * bool -> unit
Public Shared Sub StructureToPtr (structure As Object, ptr As IntPtr, fDeleteOld As Boolean)

参数

structure
Object

包含要封送的数据的托管对象。 该对象必须是格式化类的结构或实例。

ptr
IntPtr

nativeint

指向非托管内存块的指针,必须在调用此方法之前分配该指针。

fDeleteOld
Boolean

如果在此方法复制该数据前在 DestroyStructure(IntPtr, Type) 参数上调用 ptr,则为 true。 该块必须包含有效的数据。 请注意,在内存块已包含数据时传递 false 可能会导致内存泄漏。

属性

例外

structure 为不是格式化类的引用类型。

- 或 -

structure 是泛型类型的实例(仅限 .NET Framework 4.5 和更低版本)。

示例

以下示例创建一个托管结构,使用 StructureToPtr 方法将其传输到非托管内存,然后使用 方法将其传输回托管内存 PtrToStructure

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

注解

如果 structure 是值类型,则可以将其装箱或取消装箱。 如果已装箱,则会在复制前将其取消装箱。

格式化类是一种引用类型,其布局由 StructLayoutAttribute 特性指定为 LayoutKind.ExplicitLayoutKind.Sequential

StructureToPtr 将 的内容 structure 复制到参数指向的预先分配的内存 ptr 块。 如果 structure 包含封送到 COM 接口指针 (接口、没有布局的类和 System.Object) 的引用类型,则托管对象将保持活动状态并包含引用计数。 所有其他引用类型 (例如,字符串和数组) 封送为副本。 若要释放这些托管或非托管对象,必须在释放内存块之前调用 Marshal.DestroyStructure 方法。

如果以后使用 StructureToPtr 方法将其他实例复制到内存块,请指定 truefDeleteOld 删除上一实例中引用类型的引用计数。 否则,托管引用类型和非托管副本将有效泄漏。

使用 StructureToPtr 的总体模式如下所示:

  1. 在分配内存块后首次调用 StructureToPtr 方法时, fDeleteOld 必须是 false,因为没有要清除的内容。

    重要

    仅当块包含有效数据时,才指定 truefDeleteOld

  2. 如果将其他实例复制到内存块,并且对象包含引用类型, fDeleteOld 则必须释放 true 旧内容中的引用类型。

  3. 如果对象包含引用类型,则必须在释放内存块之前调用 DestroyStructure 方法。

注意

若要固定现有结构而不是复制它,请使用 System.Runtime.InteropServices.GCHandle 类型为结构创建固定句柄。 有关如何固定的详细信息,请参阅 复制和固定

另请参阅

适用于

StructureToPtr<T>(T, IntPtr, Boolean)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

将数据从指定类型的托管对象封送到非托管内存块。

public:
generic <typename T>
 static void StructureToPtr(T structure, IntPtr ptr, bool fDeleteOld);
[System.Security.SecurityCritical]
public static void StructureToPtr<T> (T structure, IntPtr ptr, bool fDeleteOld);
public static void StructureToPtr<T> (T structure, IntPtr ptr, bool fDeleteOld);
[<System.Security.SecurityCritical>]
static member StructureToPtr : 'T * nativeint * bool -> unit
static member StructureToPtr : 'T * nativeint * bool -> unit
Public Shared Sub StructureToPtr(Of T) (structure As T, ptr As IntPtr, fDeleteOld As Boolean)

类型参数

T

托管对象的类型。

参数

structure
T

包含要封送的数据的托管对象。 该对象必须是格式化类的结构或实例。

ptr
IntPtr

nativeint

指向非托管内存块的指针,必须在调用此方法之前分配该指针。

fDeleteOld
Boolean

如果在此方法复制该数据前在 DestroyStructure<T>(IntPtr) 参数上调用 ptr,则为 true。 该块必须包含有效的数据。 请注意,在内存块已包含数据时传递 false 可能会导致内存泄漏。

属性

例外

structure 为不是格式化类的引用类型。

注解

格式化类是一种引用类型,其布局由 StructLayoutAttribute 特性指定为 LayoutKind.ExplicitLayoutKind.Sequential

StructureToPtr<T>(T, IntPtr, Boolean) 将 的内容 structure 复制到参数指向的预先分配的内存 ptr 块。 如果 structure 包含封送到 COM 接口指针 (接口、没有布局的类和 System.Object) 的引用类型,则托管对象将保持活动状态并包含引用计数。 所有其他引用类型 (例如,字符串和数组) 封送为副本。 若要释放这些托管或非托管对象,必须在释放内存块之前调用 DestroyStructure<T>(IntPtr) 方法。

如果以后使用 StructureToPtr<T>(T, IntPtr, Boolean) 方法将其他实例复制到内存块,请指定 truefDeleteOld 删除上一实例中引用类型的引用计数。 否则,托管引用类型和非托管副本将有效泄漏。

使用 StructureToPtr<T>(T, IntPtr, Boolean) 的总体模式如下所示:

  1. 在分配内存块后首次调用 StructureToPtr 方法时, fDeleteOld 必须是 false,因为没有要清除的内容。

    重要

    仅当块包含有效数据时,才指定 truefDeleteOld

  2. 如果将其他实例复制到内存块,并且对象包含引用类型, fDeleteOld 则必须释放 true 旧内容中的引用类型。

  3. 如果对象包含引用类型,则必须在释放内存块之前调用 DestroyStructure 方法。

注意

若要固定现有结构而不是复制它,请使用 System.Runtime.InteropServices.GCHandle 类型为结构创建固定句柄。 有关如何固定的详细信息,请参阅 复制和固定

另请参阅

适用于