Marshal.PtrToStructure メソッド

定義

アンマネージド メモリ ブロックからマネージド オブジェクトにデータをマーシャリングします。

オーバーロード

PtrToStructure(IntPtr, Object)
古い.

アンマネージド メモリ ブロックからマネージド オブジェクトにデータをマーシャリングします。

PtrToStructure(IntPtr, Type)
古い.

アンマネージド メモリ ブロックから、指定した型の、新しく割り当てられたマネージド オブジェクトにデータをマーシャリングします。

PtrToStructure<T>(IntPtr)

アンマネージド メモリ ブロックから、ジェネリック型パラメーターによって指定された型の、新しく割り当てられたマネージド オブジェクトにデータをマーシャリングします。

PtrToStructure<T>(IntPtr, T)

アンマネージド メモリ ブロックから、指定した型のマネージド オブジェクトにデータをマーシャリングします。

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

アンマネージド メモリ ブロックからマネージド オブジェクトにデータをマーシャリングします。

C#
[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);
C#
public static void PtrToStructure (IntPtr ptr, object structure);
C#
[System.Security.SecurityCritical]
public static void PtrToStructure (IntPtr ptr, object structure);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static void PtrToStructure (IntPtr ptr, object structure);
C#
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(true)]
public static void PtrToStructure (IntPtr ptr, object structure);

パラメーター

ptr
IntPtr

アンマネージ メモリ ブロックへのポインター。

structure
Object

データのコピー先のオブジェクト。 これは、書式指定クラスのインスタンスである必要があります。

属性

例外

構造体のレイアウトがシーケンシャルまたは明示的ではありません。

または

構造体は、ボックス化された値型です。

注釈

PtrToStructure は、多くの場合、構造体パラメーターが値として表される場合に、COM 相互運用機能とプラットフォーム呼び出しで System.IntPtr 必要になります。 このオーバーロード メソッドを値型と共に使用することはできません。 パラメーターが ptr と等しいIntPtr.Zeronull場合は、 が返されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン (廃止)
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 (Core 1.0)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 2.0, 2.1 (1.2, 1.3, 1.4, 1.5, 1.6)
UWP (10.0)

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

アンマネージド メモリ ブロックから、指定した型の、新しく割り当てられたマネージド オブジェクトにデータをマーシャリングします。

C#
[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);
C#
public static object? PtrToStructure (IntPtr ptr, Type structureType);
C#
[System.Security.SecurityCritical]
public static object PtrToStructure (IntPtr ptr, Type structureType);
C#
public static object PtrToStructure (IntPtr ptr, Type structureType);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static object PtrToStructure (IntPtr ptr, Type structureType);
C#
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(true)]
public static object PtrToStructure (IntPtr ptr, Type structureType);

パラメーター

ptr
IntPtr

アンマネージ メモリ ブロックへのポインター。

structureType
Type

作成するオブジェクトの型。 このオブジェクトは、書式指定クラスまたは構造体を表す必要があります。

戻り値

ptr パラメーターが指すデータを格納しているマネージド オブジェクト。

属性

例外

structureType パラメーターのレイアウトがシーケンシャルまたは明示的ではありません。

または

structureType パラメーターがジェネリック型定義です。

structureTypenullです。

structureType で指定したクラスに、アクセスできるパラメーターなしのコンストラクターがありません。

次の例では、マネージド構造体を作成し、アンマネージド メモリに転送してから、 メソッドを使用して PtrToStructure マネージド メモリに転送します。

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

次の例では、 メソッドを使用してアンマネージ メモリ ブロックをマネージド構造体にマーシャリングする方法を PtrToStructure 示します。

重要

このコードは、32 ビットコンパイルを前提としています。 64 ビット コンパイラを使用する前に、 を にIntPtr.ToInt64置き換えますIntPtr.ToInt32

C#

        [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 は、多くの場合、構造体パラメーターが値として表される場合に、COM 相互運用機能とプラットフォーム呼び出しで System.IntPtr 必要になります。 このオーバーロード メソッドに値型を渡すことができます。 この場合、返されるオブジェクトはボックス化されたインスタンスです。 パラメーターが ptr と等しいIntPtr.Zeronull場合は、 が返されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン (廃止)
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 (Core 1.0)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 2.0, 2.1 (1.2, 1.3, 1.4, 1.5, 1.6)
UWP (10.0)

PtrToStructure<T>(IntPtr)

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ブロックから、ジェネリック型パラメーターによって指定された型の、新しく割り当てられたマネージド オブジェクトにデータをマーシャリングします。

C#
[System.Security.SecurityCritical]
public static T PtrToStructure<T> (IntPtr ptr);
C#
public static T? PtrToStructure<T> (IntPtr ptr);
C#
public static T PtrToStructure<T> (IntPtr ptr);

型パラメーター

T

データをコピーする先のオブジェクトの型。 これは、書式指定クラスまたは構造体である必要があります。

パラメーター

ptr
IntPtr

アンマネージ メモリ ブロックへのポインター。

戻り値

T

ptr パラメーターが指すデータを格納しているマネージド オブジェクト。

属性

例外

T のレイアウトがシーケンシャルまたは明示的ではありません。

T で指定したクラスに、アクセスできるパラメーターなしのコンストラクターがありません。

注釈

PtrToStructure<T>(IntPtr) は、多くの場合、構造体パラメーターが値として表される場合に、COM 相互運用機能とプラットフォーム呼び出しで System.IntPtr 必要になります。 このメソッド のオーバーロードに値型を渡すことができます。 パラメーターが ptr と等しく IntPtr.ZeroT 参照型の場合は が null 返されます。 が とIntPtr.Zero等しい場合、 T が値型である場合ptrは、 NullReferenceException がスローされます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

PtrToStructure<T>(IntPtr, T)

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ブロックから、指定した型のマネージド オブジェクトにデータをマーシャリングします。

C#
[System.Security.SecurityCritical]
public static void PtrToStructure<T> (IntPtr ptr, T structure);
C#
public static void PtrToStructure<T> (IntPtr ptr, T structure);

型パラメーター

T

structure の型。 書式が設定されたクラスである必要があります。

パラメーター

ptr
IntPtr

アンマネージ メモリ ブロックへのポインター。

structure
T

データのコピー先のオブジェクト。

属性

例外

構造体のレイアウトがシーケンシャルまたは明示的ではありません。

注釈

PtrToStructure<T>(IntPtr, T) は、多くの場合、構造体パラメーターが値として表される場合に、COM 相互運用機能とプラットフォーム呼び出しで IntPtr 必要になります。 このメソッド オーバーロードは、値型では使用できません。 パラメーターが ptr と等しく IntPtr.ZeroT 参照型の場合は が null 返されます。 が とIntPtr.Zero等しい場合、 T が値型である場合ptrは、 NullReferenceException がスローされます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0