Marshal.PtrToStructure Method (IntPtr, Type)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

Public Shared Function PtrToStructure (
	ptr As IntPtr,
	structureType As Type
) As Object


Type: System.IntPtr

A pointer to an unmanaged block of memory.

Type: System.Type

The type of object to be created. This object must represent a formatted class or a structure.

Return Value

Type: System.Object

A managed object containing the data pointed to by the ptr parameter.

Exception Condition

The structureType parameter layout is not sequential or explicit.


The structureType parameter is a generic type.


structureType is null.


The class specified by structureType does not have an accessible default constructor.

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 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))


            ' 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 + ".")

            ' Free the unmanaged memory.
        End Try

    End Sub
End Module

The following example demonstrates how to marshal an unmanaged block of memory to a managed structure using the PtrToStructure method.


This code assumes 32-bit compilation. Before using a 64-bit compiler, replace IntPtr.ToInt32 with IntPtr.ToInt64.

No code example is currently available or this language may not be supported.


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top