Marshal.PtrToStructure Method (IntPtr, Type)
Assembly: mscorlib (in mscorlib.dll)
[ComVisibleAttribute(true)] public static Object PtrToStructure ( IntPtr ptr, Type structureType )
/** @attribute ComVisibleAttribute(true) */ public static Object PtrToStructure ( IntPtr ptr, Type structureType )
ComVisibleAttribute(true) public static function PtrToStructure ( ptr : IntPtr, structureType : Type ) : Object
Parameters
- ptr
A pointer to an unmanaged block of memory.
- structureType
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.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.
Note |
|---|
| 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.
using System; using System.Runtime.InteropServices; public struct Point { public int x; public int y; } class Example { static void Main() { // Create a point struct. Point p; p.x = 1; p.y = 1; Console.WriteLine("The value of first point is " + p.x + " and " + p.y + "."); // Initialize unmanged memory to hold the struct. IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p)); try { // Copy the struct to unmanaged memory. Marshal.StructureToPtr(p, pnt, false); // Create another point. Point anotherP; // Set this Point to the value of the // Point in unmanaged memory. anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point)); Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + "."); } finally { // Free the unmanaged memory. Marshal.FreeHGlobal(pnt); } } }
The following code example demonstrates how to marshal an unmanaged block of memory to a managed structure using the PtrToStructure method.
[StructLayout(LayoutKind.Sequential)] public class INNER { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string field1 = "Test"; } [StructLayout(LayoutKind.Sequential)] public struct OUTER { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string field1; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public byte[] inner; } [DllImport(@"SomeTestDLL.dll")] public static extern void CallTest( ref OUTER po); 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.Length * iStructSize; ed.inner = new byte[sz]; try { CallTest( ref ed); } catch(Exception e) { Console.WriteLine(e.Message); } 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[i] = (INNER)Marshal.PtrToStructure(new IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) ); iCurOffset += iStructSize; } Console.WriteLine(ed.field1); Marshal.FreeCoTaskMem(buffer); }
/** @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 {
CallTest(ed);
}
catch (System.Exception e) {
Console.WriteLine(e.get_Message());
}
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;
}
Console.WriteLine(ed.field1);
Marshal.FreeCoTaskMem(buffer);
} //main
- SecurityPermission for permission to call unmanaged code. Associated enumeration: UnmanagedCode Security action: LinkDemand
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note