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)

[<ComVisibleAttribute(true)>]
static member PtrToStructure : 
        ptr:IntPtr * 
        structureType:Type -> 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 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.

ExceptionCondition
ArgumentException

The structureType parameter layout is not sequential or explicit.

-or-

The structureType parameter is a generic type.

ArgumentNullException

structureType is a null reference (Nothing in Visual Basic).

MissingMethodException

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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

  • SecurityCriticalAttribute 

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft