Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

Marshal::PtrToStructure Method (IntPtr, Type)

Note: This API is now obsolete.

Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.

Namespace:  System.Runtime.InteropServices
Assemblies:   System.Runtime.InteropServices (in System.Runtime.InteropServices.dll)
  mscorlib (in mscorlib.dll)

[ObsoleteAttribute(L"PtrToStructure(IntPtr, Type) may be unavailable in future releases. Instead, use PtrToStructure<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296513")]
static Object^ PtrToStructure(
	IntPtr ptr, 
	Type^ structureType


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.


The structureType parameter layout is not sequential or explicit.


The structureType parameter is a generic type.


structureType is nullptr.


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.

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

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

Important noteImportant

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

ref class INNER
    String^ field;

        field = "Test";

value struct OUTER
    String^ field;

    array<Byte>^ inner;

static void CallTest(OUTER^ outerStructurePointer);

void static Work()
    OUTER outerStructure;
    array<INNER^>^ innerArray = gcnew array<INNER^>(10);
    INNER^ innerStructure = gcnew INNER;
    int structSize = Marshal::SizeOf(innerStructure);
    int size = innerArray->Length * structSize;
    outerStructure.inner = gcnew array<Byte>(size);

    catch (SystemException^ ex) 

    IntPtr buffer = Marshal::AllocCoTaskMem(structSize * 10);
    Marshal::Copy(outerStructure.inner, 0, buffer, structSize * 10);
    int currentOffset = 0;
    for (int i = 0; i < 10; i++)
        innerArray[i] = safe_cast<INNER^>(Marshal::PtrToStructure(
            IntPtr(buffer.ToInt32() + currentOffset),
        currentOffset += structSize;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Obsolete (compiler warning) in 4.6

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

  • SecurityCriticalAttribute 

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

© 2015 Microsoft