Marshal::PtrToStructure Method (IntPtr, Type^)
Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.
Assembly: mscorlib (in mscorlib.dll)
public: [SecurityCriticalAttribute] [ComVisibleAttribute(true)] static Object^ PtrToStructure( IntPtr ptr, Type^ structureType )
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.
| Exception | Condition |
|---|---|
| ArgumentException | The structureType parameter layout is not sequential or explicit. -or- The structureType parameter is a generic type. |
| ArgumentNullException | structureType is null. |
| 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.
The following example demonstrates how to marshal an unmanaged block of memory to a managed structure using the PtrToStructure method.
Important |
|---|
This code assumes 32-bit compilation. Before using a 64-bit compiler, replace IntPtr::ToInt32 with IntPtr::ToInt64. |
[StructLayout(LayoutKind::Sequential)] ref class INNER { public: [MarshalAs(UnmanagedType::ByValTStr,SizeConst=10)] String^ field; INNER() { field = "Test"; } }; [StructLayout(LayoutKind::Sequential)] value struct OUTER { public: [MarshalAs(UnmanagedType::ByValTStr,SizeConst=10)] String^ field; [MarshalAs(UnmanagedType::ByValArray,SizeConst=100)] array<Byte>^ inner; }; [DllImport("SomeTestDLL.dll")] 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); try { CallTest(outerStructure); } catch (SystemException^ ex) { Console::WriteLine(ex->Message); } 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), INNER::typeid)); currentOffset += structSize; } Console::WriteLine(outerStructure.field); Marshal::FreeCoTaskMem(buffer); }
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
