Marshal.StructureToPtr メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Marshal.StructureToPtr メソッドの意味・解説 

Marshal.StructureToPtr メソッド

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

名前空間: System.Runtime.InteropServices
アセンブリ: 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
)

パラメータ

structure

マーシャリングするデータを保持しているマネージ オブジェクト。このオブジェクトは、書式指定クラスのインスタンスである必要があります。

ptr

このメソッドを呼び出す前に割り当てる必要があるアンマネージ メモリ ブロックへのポインタ。

fDeleteOld

このメソッドの実行前に、ptr パラメータに対して Marshal.DestroyStructure メソッドを呼び出す場合は true。false を渡すと、メモリ リークが生じる場合があります。

例外例外
解説解説

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 型を使用して、構造体の固定ハンドルを作成します。固定方法の詳細については、「コピーと固定」を参照してください。

メモメモ

このメソッドは SecurityAction.LinkDemand を使用して、信頼関係のないコードからの呼び出しを防ぎます。SecurityPermissionAttribute.UnmanagedCode アクセス許可は、直前の呼び出し元にのみ要求されます。信頼性が一部しか確認されていないコードから呼び出すことができるコードの場合、ユーザー入力を検証せずに Marshal クラスに渡すことは避けてください。LinkDemand メンバの使用に関する重要な制約事項については、「Demand と LinkDemand」を参照してください。

使用例使用例

マネージ構造体を作成し、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);
        }
        


    }

}
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Marshal クラス
Marshal メンバ
System.Runtime.InteropServices 名前空間
DestroyStructure
GCHandle 構造体



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Marshal.StructureToPtr メソッド」の関連用語

Marshal.StructureToPtr メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Marshal.StructureToPtr メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS