Marshal.StructureToPtr メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public Shared Sub StructureToPtr ( _ structure As Object, _ ptr As IntPtr, _ fDeleteOld As Boolean _ )
Dim structure As Object Dim ptr As IntPtr Dim fDeleteOld As Boolean Marshal.StructureToPtr(structure, ptr, fDeleteOld)
[ComVisibleAttribute(true)] public static void StructureToPtr ( Object structure, IntPtr ptr, bool fDeleteOld )
[ComVisibleAttribute(true)] public: static void StructureToPtr ( Object^ structure, IntPtr ptr, bool fDeleteOld )
/** @attribute ComVisibleAttribute(true) */ public static void StructureToPtr ( Object structure, IntPtr ptr, boolean fDeleteOld )
ComVisibleAttribute(true) public static function StructureToPtr ( structure : Object, ptr : IntPtr, fDeleteOld : boolean )


StructureToPtr は、ptr パラメータが指す、事前に割り当てられたメモリ ブロックに構造体の内容をコピーします。fDeleteOld パラメータが true の場合、ptr が最初に指していたバッファは、埋め込みポインタに対する適切な削除 API を呼び出すことによって削除されます。ただし、バッファには有効なデータが格納されている必要があります。このメソッドは、反映されたマネージ クラスで指定されたすべての参照フィールドをクリーンアップします。
アンマネージ メモリ ブロックが ptr によって指定されるとします。このブロックのレイアウトは、対応するマネージ クラス structure によって記述されます。StructureToPtr は、構造体からポインタにフィールド値をマーシャリングします。ptr ブロックが参照フィールドを含み、現在 "abc" を保持する文字列バッファを指しているとします。マネージ側の対応するフィールドは、"vwxyz" を保持する文字列であるとします。特に何も指定しない場合は、StructureToPtr が "vwxyz" を保持する新しいアンマネージ バッファを割り当て、ptr ブロックにフックします。これにより、"abc" を保持している古いバッファは、解放されてアンマネージ ヒープに戻されることなく、宙に浮きます。最終的には、コード内のメモリ リークを表す孤立したバッファとなります。fDeleteOld パラメータを true に設定した場合、StructureToPtr は "abc" を保持するバッファを解放してから、"vwxyz" の新しいバッファを割り当てます。
![]() |
---|
既存の構造体を固定するには、コピーする代わりに、System.Runtime.InteropServices.GCHandle 型を使用して、構造体の固定ハンドルを作成します。固定方法の詳細については、「コピーと固定」を参照してください。 |

マネージ構造体を作成し、StructureToPtr メソッドを使用してアンマネージ メモリに転送した後、PtrToStructure メソッドを使用してマネージ メモリに再度転送するコード例を次に示します。
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
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); } } }

- SecurityPermission (アンマネージ コードを呼び出すために必要なアクセス許可)。 UnmanagedCode (関連する列挙体)。LinkDemand (セキュリティ アクション)。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からMarshal.StructureToPtr メソッドを検索する場合は、下記のリンクをクリックしてください。

- Marshal.StructureToPtr メソッドのページへのリンク