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 メソッド呼び出す場合truefalse を渡すと、メモリ リーク生じ場合あります

例外例外
解説解説

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 構造体


このページでは「.NET Framework クラス ライブラリ リファレンス」からMarshal.StructureToPtr メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMarshal.StructureToPtr メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMarshal.StructureToPtr メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

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

   

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



Marshal.StructureToPtr メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS