您的位置:首页 > 编程语言 > VB

VB.NET用Marshal.PtrToStructure 方法实现数据在数组和结构间复制

2013-03-20 13:41 971 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: