Edit

Share via


Marshal.PtrToStructure Method

Definition

Marshals data from an unmanaged block of memory to a managed object.

Overloads

PtrToStructure(IntPtr, Object)
Obsolete.

Marshals data from an unmanaged block of memory to a managed object.

PtrToStructure(IntPtr, Type)
Obsolete.

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

PtrToStructure<T>(IntPtr)

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

PtrToStructure<T>(IntPtr, T)

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

PtrToStructure(IntPtr, Object)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Caution

PtrToStructure(IntPtr, Object) may be unavailable in future releases. Instead, use PtrToStructure<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296512

Marshals data from an unmanaged block of memory to a managed object.

public:
 static void PtrToStructure(IntPtr ptr, System::Object ^ structure);
[System.Obsolete("PtrToStructure(IntPtr, Object) may be unavailable in future releases. Instead, use PtrToStructure<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296512")]
[System.Security.SecurityCritical]
public static void PtrToStructure(IntPtr ptr, object structure);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void PtrToStructure(IntPtr ptr, object structure);
public static void PtrToStructure(IntPtr ptr, object structure);
[System.Security.SecurityCritical]
public static void PtrToStructure(IntPtr ptr, object structure);
[System.Runtime.InteropServices.ComVisible(true)]
public static void PtrToStructure(IntPtr ptr, object structure);
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(true)]
public static void PtrToStructure(IntPtr ptr, object structure);
[<System.Obsolete("PtrToStructure(IntPtr, Object) may be unavailable in future releases. Instead, use PtrToStructure<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296512")>]
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
static member PtrToStructure : nativeint * obj -> unit
static member PtrToStructure : nativeint * obj -> unit
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint * obj -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
static member PtrToStructure : nativeint * obj -> unit
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(true)>]
static member PtrToStructure : nativeint * obj -> unit
Public Shared Sub PtrToStructure (ptr As IntPtr, structure As Object)

Parameters

ptr
IntPtr

nativeint

A pointer to an unmanaged block of memory.

structure
Object

The object to which the data is to be copied. This must be an instance of a formatted class.

Attributes

Exceptions

Structure layout is not sequential or explicit.

-or-

Structure is a boxed value type.

Remarks

PtrToStructure is often necessary in COM interop and platform invoke when structure parameters are represented as an System.IntPtr value. You cannot use this overload method with value types. If the ptr parameter equals IntPtr.Zero, null will be returned.

Applies to

PtrToStructure(IntPtr, Type)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Caution

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

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

public:
 static System::Object ^ PtrToStructure(IntPtr ptr, Type ^ structureType);
[System.Obsolete("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")]
[System.Security.SecurityCritical]
public static object PtrToStructure(IntPtr ptr, Type structureType);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
public static object? PtrToStructure(IntPtr ptr, Type structureType);
public static object? PtrToStructure(IntPtr ptr, Type structureType);
[System.Security.SecurityCritical]
public static object PtrToStructure(IntPtr ptr, Type structureType);
public static object PtrToStructure(IntPtr ptr, Type structureType);
[System.Runtime.InteropServices.ComVisible(true)]
public static object PtrToStructure(IntPtr ptr, Type structureType);
[System.Security.SecurityCritical]
[System.Runtime.InteropServices.ComVisible(true)]
public static object PtrToStructure(IntPtr ptr, Type structureType);
[<System.Obsolete("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")>]
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint * Type -> obj
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
static member PtrToStructure : nativeint * Type -> obj
static member PtrToStructure : nativeint * Type -> obj
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint * Type -> obj
[<System.Runtime.InteropServices.ComVisible(true)>]
static member PtrToStructure : nativeint * Type -> obj
[<System.Security.SecurityCritical>]
[<System.Runtime.InteropServices.ComVisible(true)>]
static member PtrToStructure : nativeint * Type -> obj
Public Shared Function PtrToStructure (ptr As IntPtr, structureType As Type) As Object

Parameters

ptr
IntPtr

nativeint

A pointer to an unmanaged block of memory.

structureType
Type

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

Returns

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

Attributes

Exceptions

The structureType parameter layout is not sequential or explicit.

-or-

The structureType parameter is a generic type definition.

structureType is null.

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

Remarks

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. If the ptr parameter equals IntPtr.Zero, null will be returned.

See also

Applies to

PtrToStructure<T>(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

public:
generic <typename T>
 static T PtrToStructure(IntPtr ptr);
[System.Security.SecurityCritical]
public static T PtrToStructure<T>(IntPtr ptr);
public static T? PtrToStructure<T>(IntPtr ptr);
public static T PtrToStructure<T>(IntPtr ptr);
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint -> 'T
static member PtrToStructure : nativeint -> 'T
Public Shared Function PtrToStructure(Of T) (ptr As IntPtr) As T

Type Parameters

T

The type of the object to which the data is to be copied. This must be a formatted class or a structure.

Parameters

ptr
IntPtr

nativeint

A pointer to an unmanaged block of memory.

Returns

T

A managed object that contains the data that the ptr parameter points to.

Attributes

Exceptions

The layout of T is not sequential or explicit.

The class specified by T does not have an accessible parameterless constructor.

Remarks

PtrToStructure<T>(IntPtr) is often necessary in COM interop and platform invoke when structure parameters are represented as System.IntPtr values. You can pass a value type to this method overload. If the ptr parameter equals IntPtr.Zero and T is a reference type, null is returned. If ptr equals IntPtr.Zero and T is a value type, a NullReferenceException is thrown.

Applies to

PtrToStructure<T>(IntPtr, T)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

public:
generic <typename T>
 static void PtrToStructure(IntPtr ptr, T structure);
[System.Security.SecurityCritical]
public static void PtrToStructure<T>(IntPtr ptr, T structure);
public static void PtrToStructure<T>(IntPtr ptr, T structure);
[<System.Security.SecurityCritical>]
static member PtrToStructure : nativeint * 'T -> unit
static member PtrToStructure : nativeint * 'T -> unit
Public Shared Sub PtrToStructure(Of T) (ptr As IntPtr, structure As T)

Type Parameters

T

The type of structure. This must be a formatted class.

Parameters

ptr
IntPtr

nativeint

A pointer to an unmanaged block of memory.

structure
T

The object to which the data is to be copied.

Attributes

Exceptions

Structure layout is not sequential or explicit.

Remarks

PtrToStructure<T>(IntPtr, T) is often necessary in COM interop and platform invoke when structure parameters are represented as IntPtr values. You cannot use this method overload with value types. If the ptr parameter equals IntPtr.Zero and T is a reference type, null is returned. If ptr equals IntPtr.Zero and T is a value type, a NullReferenceException is thrown.

Applies to