Marshal.PtrToStructure 方法

定義

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

多載

PtrToStructure(IntPtr, Object)
已淘汰.

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

PtrToStructure(IntPtr, Type)
已淘汰.

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

PtrToStructure<T>(IntPtr)

從 Unmanaged 記憶體區塊封送處理資料到新配置的指定類型的 Managed 物件 (其類型是由泛型類型參數所指定)。

PtrToStructure<T>(IntPtr, T)

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

PtrToStructure(IntPtr, Object)

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

警告

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

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

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

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標。

structure
Object

複製資料所到的物件。 這必須是格式化類別的執行個體。

屬性

例外狀況

結構配置不是循序或明確的。

-或-

結構為 Boxed 實值類型。

備註

PtrToStructure 當結構參數以 System.IntPtr 值表示時,通常必須在 COM Interop 和平臺叫用中。 您無法搭配實值型別使用此多載方法。 ptr如果 參數等於 IntPtr.Zeronull 則會傳回 。

適用於

PtrToStructure(IntPtr, Type)

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

警告

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

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

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

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標。

structureType
Type

要建立的物件類型。 這個物件必須表示格式化類別或結構。

傳回

包含 ptr 參數所指向的資料的 Managed 物件。

屬性

例外狀況

structureType 參數配置不是循序或明確的。

-或-

structureType 參數是泛型型別定義。

structureTypenull

structureType 所指定類別沒有可存取的無參數建構函式。

範例

下列範例會建立 Managed 結構、將其傳輸至 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

下列範例示範如何使用 方法,將非受控記憶體區塊封送處理至 Managed 結構 PtrToStructure

重要

此程式碼假設 32 位編譯。 使用 64 位編譯器之前,請將 取代 IntPtr.ToInt32IntPtr.ToInt64

[StructLayout(LayoutKind::Sequential)]
ref class INNER
{
public:
    [MarshalAs(UnmanagedType::ByValTStr,SizeConst=10)]
    String^ field;

    INNER()
    {
        field = "Test";
    }
};

[StructLayout(LayoutKind::Sequential)]
value struct OUTER
{
public:
    [MarshalAs(UnmanagedType::ByValTStr,SizeConst=10)]
    String^ field;

    [MarshalAs(UnmanagedType::ByValArray,SizeConst=100)]
    array<Byte>^ inner;
};

[DllImport("SomeTestDLL.dll")]
static void CallTest(OUTER^ outerStructurePointer);

void static Work()
{
    OUTER outerStructure;
    array<INNER^>^ innerArray = gcnew array<INNER^>(10);
    INNER^ innerStructure = gcnew INNER;
    int structSize = Marshal::SizeOf(innerStructure);
    int size = innerArray->Length * structSize;
    outerStructure.inner = gcnew array<Byte>(size);

    try
    {
        CallTest(outerStructure);
    }
    catch (SystemException^ ex) 
    {
        Console::WriteLine(ex->Message);
    }

    IntPtr buffer = Marshal::AllocCoTaskMem(structSize * 10);
    Marshal::Copy(outerStructure.inner, 0, buffer, structSize * 10);
    int currentOffset = 0;
    for (int i = 0; i < 10; i++)
    {
        innerArray[i] = safe_cast<INNER^>(Marshal::PtrToStructure(
            IntPtr(buffer.ToInt32() + currentOffset),
            INNER::typeid));
        currentOffset += structSize;
    }
    Console::WriteLine(outerStructure.field);
    Marshal::FreeCoTaskMem(buffer);
}

        [StructLayout(LayoutKind.Sequential)]

        public class  INNER

        {

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst =  10)]

            public string field1 = "Test";
        }	

        [StructLayout(LayoutKind.Sequential)]

        public struct OUTER

        {

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst =  10)]

            public string field1;

            [MarshalAs(UnmanagedType.ByValArray, SizeConst =  100)]

            public byte[] inner;
        }
        [DllImport(@"SomeTestDLL.dll")]

        public static extern void CallTest( ref OUTER po);
        static void Main(string[] args)

        {

            OUTER ed = new OUTER();

            INNER[] inn=new INNER[10];

            INNER test = new INNER();

            int iStructSize = Marshal.SizeOf(test);
            int sz =inn.Length * iStructSize;

            ed.inner = new byte[sz];
            try

            {

                CallTest( ref ed);
            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);
            }

            IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize*10);

            Marshal.Copy(ed.inner,0,buffer,iStructSize*10);
            int iCurOffset = 0;

            for(int i=0;i<10;i++)

            {
                inn[i] = (INNER)Marshal.PtrToStructure(new
IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) );

                iCurOffset += iStructSize;
            }

            Console.WriteLine(ed.field1);

            Marshal.FreeCoTaskMem(buffer);
        }

備註

PtrToStructure 當結構參數以 System.IntPtr 值表示時,通常必須在 COM Interop 和平臺叫用中。 您可以將實值型別傳遞至這個多載方法。 在此情況下,傳回的物件是 Boxed 實例。 ptr如果 參數等於 IntPtr.Zeronull 則會傳回 。

另請參閱

適用於

PtrToStructure<T>(IntPtr)

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

從 Unmanaged 記憶體區塊封送處理資料到新配置的指定類型的 Managed 物件 (其類型是由泛型類型參數所指定)。

public:
generic <typename T>
 static T PtrToStructure(IntPtr ptr);
[System.Security.SecurityCritical]
public static T PtrToStructure<T> (IntPtr ptr);
public static T? PtrToStructure<T> (IntPtr ptr);
public static T PtrToStructure<T> (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint -> 'T
static member PtrToStructure : nativeint -> 'T
Public Shared Function PtrToStructure(Of T) (ptr As IntPtr) As T

類型參數

T

複製資料所到的物件類型。 這必須是格式化類別或結構。

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標。

傳回

T

包含 ptr 參數所指向的資料的 Managed 物件。

屬性

例外狀況

T 的配置不是循序或明確的。

T 所指定類別沒有可存取的無參數建構函式。

備註

PtrToStructure<T>(IntPtr) 當結構參數以 System.IntPtr 值表示時,通常必須在 COM Interop 和平臺叫用中。 您可以將實值型別傳遞至這個方法多載。 ptr如果 參數等於 IntPtr.ZeroT 為參考型別, null 則會傳回 。 如果 ptr 等於 IntPtr.ZeroT 是實值型別, NullReferenceException 則會擲回 。

適用於

PtrToStructure<T>(IntPtr, T)

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

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

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

類型參數

T

structure 的類型。 這必須是格式化類別。

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體區塊的指標。

structure
T

複製資料所到的物件。

屬性

例外狀況

結構配置不是循序或明確的。

備註

PtrToStructure<T>(IntPtr, T) 當結構參數以 IntPtr 值表示時,通常必須在 COM Interop 和平臺叫用中。 您無法搭配實值型別使用這個方法多載。 ptr如果 參數等於 IntPtr.ZeroT 為參考型別, null 則會傳回 。 如果 ptr 等於 IntPtr.ZeroT 是實值型別, NullReferenceException 則會擲回 。

適用於