Marshal.PtrToStructure Method (IntPtr, Type)

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)

static Object^ PtrToStructure (
	IntPtr ptr, 
	Type^ structureType
/** @attribute ComVisibleAttribute(true) */ 
public static Object PtrToStructure (
	IntPtr ptr, 
	Type structureType
public static function PtrToStructure (
	ptr : IntPtr, 
	structureType : Type
) : Object
Not applicable.



A pointer to an unmanaged block of memory.


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

Return Value

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

Exception typeCondition


The structureType parameter layout is not sequential or explicit.


The structureType parameter is a generic type.


structureType is a 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.


This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

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.

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

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

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;

/** @attribute StructLayout(LayoutKind.Sequential)
public static class Inner
    /** @attribute MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)
    public String field1 = "Test";
} //Inner

/** @attribute StructLayout(LayoutKind.Sequential)
public static class Outer
    /** @attribute MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)
    public String field1;

    /** @attribute MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)
    public ubyte inner[];
} //Outer

/** @attribute DllImport("SomeTestDLL.dll")
public static native void CallTest(Outer po);

private static void main(String[] args)
    Outer ed = new Outer();
    Inner inn[] = new Inner[10];
    Inner test = new Inner();
    int iStructSize = Marshal.SizeOf(test);
    int sz = inn.get_Length() * iStructSize;
    ed.inner = new ubyte[sz];
    try {
    catch (System.Exception e) {
    IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize * 10);
    Marshal.Copy(ed.inner, 0, buffer, iStructSize * 10);
    int iCurOffset = 0;
    for (int i = 0; i < 10; i++) {
        inn.set_Item(i, (Inner)(Marshal.PtrToStructure(new IntPtr(
            buffer.ToInt32() + iCurOffset), Inner.class.ToType())));
        iCurOffset += iStructSize;
} //main 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions