Marshal.StructureToPtr 方法

定義

多載

StructureToPtr(Object, IntPtr, Boolean)
已淘汰.

從 Managed 物件封送處理資料到 Unmanaged 記憶體區塊。

StructureToPtr<T>(T, IntPtr, Boolean)

從指定類型的 Managed 物件封送處理資料到 Unmanaged 記憶體區塊。

StructureToPtr(Object, IntPtr, Boolean)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
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

從 Managed 物件封送處理資料到 Unmanaged 記憶體區塊。

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

存有要封送處理之資料的 Managed 物件。 這個物件必須是格式化類別的結構或執行個體。

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標,這記憶體必須在呼叫這個方法之前被配置。

fDeleteOld
Boolean

true 表示在此方法複製資料之前,在 ptr 參數上呼叫 DestroyStructure(IntPtr, Type) 方法。 區塊必須包含有效的資料。 請注意,當記憶體區塊已經包含資料時傳遞 false 會造成記憶體流失。

屬性

例外狀況

structure 為非格式化類別的參考型別。

-或-

structure 是泛型型別的執行個體 (僅限 .NET Framework 4.5 和較舊版本)。

範例

下列範例會建立Managed結構、使用 StructureToPtr 方法將它傳輸至 Unmanaged 記憶體,然後使用 方法將它轉回 Managed 記憶體 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 是實值類型,則可以進行 Boxed 或 unboxed。 如果是 Boxed,則會在複製之前取消收件匣。

格式化類別是參考型別,其版面配置是由 StructLayoutAttribute 屬性指定為 LayoutKind.ExplicitLayoutKind.Sequential

StructureToPtr 將的內容 structure 複製到 參數指向的預先配置的記憶體 ptr 區塊。 如果 structure 包含封送處理至 COM 介面指標的參考型別, (介面、不含配置的類別,以及 System.Object) ,則 Managed 物件會與參考計數保持運作。 例如,所有其他參考類型 (,字串和陣列) 會封送處理為複製。 若要釋放這些 Managed 或 Unmanaged 物件,您必須先呼叫 Marshal.DestroyStructure 方法,才能釋放記憶體區塊。

如果您使用 StructureToPtr 方法,稍後將不同的實例複製到記憶體區塊,請指定 truefDeleteOld 移除先前實例中參考類型的參考計數。 否則,Managed 參考類型和 Unmanaged 複本會有效地外洩。

使用 StructureToPtr 的整體模式如下所示:

  1. 在配置記憶體區塊之後,第一次呼叫 StructureToPtr 方法時,必須是 falsefDeleteOld因為沒有要清除的內容。

    重要

    只有當 區塊包含有效的資料時,才指定 truefDeleteOld

  2. 如果您將不同的實例複製到記憶體區塊,而且物件包含參考型別, fDeleteOld 則必須在 true 舊內容中釋放參考型別。

  3. 如果物件包含參考型別,您必須先呼叫 方法, DestroyStructure 才能釋放記憶體區塊。

注意

若要釘選現有的結構,而不是複製它,請使用 System.Runtime.InteropServices.GCHandle 類型來建立結構的固定句柄。 如需如何釘選的詳細資訊,請參閱 複製和釘選

另請參閱

適用於

StructureToPtr<T>(T, IntPtr, Boolean)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從指定類型的 Managed 物件封送處理資料到 Unmanaged 記憶體區塊。

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

Managed 物件的類型。

參數

structure
T

存有要封送處理之資料的 Managed 物件。 物件必須是格式化類別的結構或執行個體。

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標,這記憶體必須在呼叫這個方法之前被配置。

fDeleteOld
Boolean

true 表示在此方法複製資料之前,在 ptr 參數上呼叫 DestroyStructure<T>(IntPtr) 方法。 區塊必須包含有效的資料。 請注意,當記憶體區塊已經包含資料時傳遞 false 會造成記憶體流失。

屬性

例外狀況

structure 為非格式化類別的參考型別。

備註

格式化類別是參考型別,其版面配置是由 StructLayoutAttribute 屬性指定為 LayoutKind.ExplicitLayoutKind.Sequential

StructureToPtr<T>(T, IntPtr, Boolean) 將的內容 structure 複製到 參數指向的預先配置的記憶體 ptr 區塊。 如果 structure 包含封送處理至 COM 介面指標的參考型別, (介面、不含配置的類別,以及 System.Object) ,則 Managed 物件會與參考計數保持運作。 例如,所有其他參考類型 (,字串和陣列) 會封送處理為複製。 若要釋放這些 Managed 或 Unmanaged 物件,您必須先呼叫 DestroyStructure<T>(IntPtr) 方法,才能釋放記憶體區塊。

如果您使用 StructureToPtr<T>(T, IntPtr, Boolean) 方法,稍後將不同的實例複製到記憶體區塊,請指定 truefDeleteOld 移除先前實例中參考類型的參考計數。 否則,Managed 參考類型和 Unmanaged 複本會有效地外洩。

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

  1. 在配置記憶體區塊之後,第一次呼叫 StructureToPtr 方法時,必須是 falsefDeleteOld因為沒有要清除的內容。

    重要

    只有當 區塊包含有效的資料時,才指定 truefDeleteOld

  2. 如果您將不同的實例複製到記憶體區塊,而且物件包含參考型別, fDeleteOld 則必須在 true 舊內容中釋放參考型別。

  3. 如果物件包含參考型別,您必須先呼叫 方法, DestroyStructure 才能釋放記憶體區塊。

注意

若要釘選現有的結構,而不是複製它,請使用 System.Runtime.InteropServices.GCHandle 類型來建立結構的固定句柄。 如需如何釘選的詳細資訊,請參閱 複製和釘選

另請參閱

適用於