SSブログ

Marshal.PtrToStructure メソッド (IntPtr, Type) VBコーディングで悩んだこと [開発環境]

C#版のサンプルコードを参考にVBでコーディングしても、期待した結果が得られなかった原因がMarshal.PtrToStructure の使用方法にあったので記録しておく。

CTypeを使用し、指定の構造体型に式を変換する必要があった。


' マネージ構造体を作成し、アンマネージ メモリに転送した後、PtrToStructure メソッドを使用してマネージ メモリに再度転送するコード例。


Imports System
Imports System.Runtime.InteropServices

' 構造体
Public Structure Point
    Public x As Integer
    Public y As Integer
End Structure


Module Module1

    Sub Main()
       ' 構造体の宣言
        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 + ".")

       ' 構造体用のアンマネージメモリを確保
        Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p))

        Try

           ' 構造体をアンマネージメモリにコピー
            Marshal.StructureToPtr(p, pnt, False)

            ' 別の構造体宣言
            Dim anotherP As Point

           ' アンマネージデータをマネージ構造体にマーシャリング(データ変換)
            anotherP = CType(Marshal.PtrToStructure(pnt, GetType(Point)), Point)
           
' C#の場合、以下の記述となるので要注意
            ' anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));
            ' または
            ' Marshal.PtrToStructure(pnt, anotherP.GetType());
            ' となるので注意

            Console.WriteLine("The value of new point is " + anotherP.x.ToString + " and " + anotherP.y.ToString + ".")

        Finally
           ' アンマネージメモリの解放
            Marshal.FreeHGlobal(pnt)
        End Try

        Console.ReadKey()
    End Sub

End Module

 

 

参考サイト


この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。