Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.
Namespace:
System.Runtime.InteropServices
Assembly:
mscorlib (in mscorlib.dll)
'Usage
Dim ptr As IntPtr
Dim structureType As Type
Dim returnValue As Object
returnValue = Marshal.PtrToStructure(ptr, _
structureType)
'Declaration
<ComVisibleAttribute(True)> _
<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _
Public Shared Function PtrToStructure ( _
ptr As IntPtr, _
structureType As Type _
) As Object
Parameters
- ptr
- Type: System..::.IntPtr
A pointer to an unmanaged block of memory.
- structureType
- Type: System..::.Type
The Type of object to be created. This type object must represent a formatted class or a structure.
Return Value
Type:
System..::.ObjectA managed object containing the data pointed to by the ptr parameter.
| Exception | Condition |
|---|
| ArgumentException | The structureType parameter layout is not sequential or explicit. -or- The structureType parameter is a generic type. |
| ArgumentNullException |
structureType is nullNothingnullptra null reference (Nothing in Visual Basic). |
PtrToStructure is often necessary in COM interop and platform invoke when structure parameters are represented as an System..::.IntPtr value. You can pass a value type to this overload method. In this case, the returned object is a boxed instance.
The following code example creates a managed structure, transfers it to unmanaged memory, and then transfers it back to managed memory using the PtrToStructure method.
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
The following code example demonstrates how to marshal an unmanaged block of memory to a managed structure using the PtrToStructure method.
We do not yet have a code sample for this language.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference