Marshal.PtrToStructure Yöntem

Tanım

Yönetilmeyen bir bellek bloğundaki verileri yönetilen bir nesneye sıralar.

Aşırı Yüklemeler

PtrToStructure(IntPtr, Object)
Geçersiz.

Yönetilmeyen bir bellek bloğundaki verileri yönetilen bir nesneye sıralar.

PtrToStructure(IntPtr, Type)
Geçersiz.

Yönetilmeyen bir bellek bloğundaki verileri, belirtilen türde yeni ayrılan yönetilen bir nesneye sıralar.

PtrToStructure<T>(IntPtr)

Yönetilmeyen bir bellek bloğundaki verileri, genel tür parametresi tarafından belirtilen türdeki yeni ayrılan yönetilen nesneye sıralar.

PtrToStructure<T>(IntPtr, T)

Yönetilmeyen bir bellek bloğundaki verileri belirtilen türde yönetilen bir nesneye sıralar.

PtrToStructure(IntPtr, Object)

Kaynak:
Marshal.cs
Kaynak:
Marshal.cs
Kaynak:
Marshal.cs

Dikkat

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

Yönetilmeyen bir bellek bloğundaki verileri yönetilen bir nesneye sıralar.

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);
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
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)

Parametreler

ptr
IntPtr

nativeint

Yönetilmeyen bir bellek bloğunun işaretçisi.

structure
Object

Verilerin kopyalandığı nesne. Bu, biçimlendirilmiş bir sınıfın örneği olmalıdır.

Öznitelikler

Özel durumlar

Yapı düzeni sıralı veya açık değil.

-veya-

Yapı, kutulu bir değer türüdür.

Açıklamalar

PtrToStructure genellikle yapı parametreleri bir System.IntPtr değer olarak temsil edildiğinde COM birlikte çalışma ve platform çağırmada gereklidir. Bu aşırı yükleme yöntemini değer türleriyle kullanamazsınız. ptr parametresi eşitse IntPtr.Zeronull döndürülür.

Şunlara uygulanır

PtrToStructure(IntPtr, Type)

Kaynak:
Marshal.cs
Kaynak:
Marshal.cs
Kaynak:
Marshal.cs

Dikkat

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

Yönetilmeyen bir bellek bloğundaki verileri, belirtilen türde yeni ayrılan yönetilen bir nesneye sıralar.

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);
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
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

Parametreler

ptr
IntPtr

nativeint

Yönetilmeyen bir bellek bloğunun işaretçisi.

structureType
Type

Oluşturulacak nesnenin türü. Bu nesne biçimlendirilmiş bir sınıfı veya bir yapıyı temsil etmelidir.

Döndürülenler

parametresi tarafından ptr işaret edilen verileri içeren yönetilen nesne.

Öznitelikler

Özel durumlar

structureType Parametre düzeni sıralı veya açık değil.

-veya-

structureType parametresi genel bir tür tanımıdır.

structureType, null değeridir.

tarafından structureType belirtilen sınıfın erişilebilir bir parametresiz oluşturucu yok.

Örnekler

Aşağıdaki örnek yönetilen bir yapı oluşturur, yönetilmeyen belleğe aktarır ve ardından yöntemini kullanarak PtrToStructure yönetilen belleğe geri aktarır.

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);
        }
    }
}
Imports System.Runtime.InteropServices



Public Structure Point
    Public x As Integer
    Public y As Integer
End Structure


Module Example


    Sub Main()

        ' Create a point struct.
        Dim p As Point
        p.x = 1
        p.y = 1

        Console.WriteLine("The value of first point is " + p.x.ToString + " and " + p.y.ToString + ".")

        ' Initialize unmanged memory to hold the struct.
        Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p))

        Try

            ' Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, False)

            ' Create another point.
            Dim anotherP As Point

            ' Set this Point to the value of the 
            ' Point in unmanaged memory. 
            anotherP = CType(Marshal.PtrToStructure(pnt, GetType(Point)), Point)

            Console.WriteLine("The value of new point is " + anotherP.x.ToString + " and " + anotherP.y.ToString + ".")

        Finally
            ' Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt)
        End Try

    End Sub
End Module

Aşağıdaki örnekte, yönetilmeyen bir bellek bloğunun yöntemini kullanarak yönetilen bir yapıya nasıl sıralanmış olduğu gösterilmektedir PtrToStructure .

Önemli

Bu kod 32 bit derleme olduğunu varsayar. 64 bit derleyici kullanmadan önce değerini ile IntPtr.ToInt64değiştirinIntPtr.ToInt32.

[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);
}

        [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);
        }

Açıklamalar

PtrToStructure genellikle yapı parametreleri bir System.IntPtr değer olarak temsil edildiğinde COM birlikte çalışma ve platform çağırmada gereklidir. Bu aşırı yükleme yöntemine bir değer türü geçirebilirsiniz. Bu durumda, döndürülen nesne kutulu bir örnektir. ptr parametresi eşitse IntPtr.Zeronull döndürülür.

Ayrıca bkz.

Şunlara uygulanır

PtrToStructure<T>(IntPtr)

Kaynak:
Marshal.cs
Kaynak:
Marshal.cs
Kaynak:
Marshal.cs

Yönetilmeyen bir bellek bloğundaki verileri, genel tür parametresi tarafından belirtilen türdeki yeni ayrılan yönetilen nesneye sıralar.

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

Tür Parametreleri

T

Verilerin kopyalandığı nesnenin türü. Bu biçimlendirilmiş bir sınıf veya yapı olmalıdır.

Parametreler

ptr
IntPtr

nativeint

Yönetilmeyen bir bellek bloğunun işaretçisi.

Döndürülenler

T

Parametrenin işaret ettiğini ptr verileri içeren yönetilen nesne.

Öznitelikler

Özel durumlar

düzeni T sıralı veya açık değil.

tarafından T belirtilen sınıfın erişilebilir bir parametresiz oluşturucu yok.

Açıklamalar

PtrToStructure<T>(IntPtr) genellikle yapı parametreleri değer olarak System.IntPtr temsil edildiğinde COM birlikte çalışma ve platform çağırmada gereklidir. Bu yöntem aşırı yüklemesine bir değer türü geçirebilirsiniz. ptr Parametresi eşitse IntPtr.Zero ve T bir başvuru türüyse döndürülürnull. eşitse ptrIntPtr.Zero ve T bir değer türüyse, bir NullReferenceException oluşturulur.

Şunlara uygulanır

PtrToStructure<T>(IntPtr, T)

Kaynak:
Marshal.cs
Kaynak:
Marshal.cs
Kaynak:
Marshal.cs

Yönetilmeyen bir bellek bloğundaki verileri belirtilen türde yönetilen bir nesneye sıralar.

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)

Tür Parametreleri

T

türü structure. Bu biçimlendirilmiş bir sınıf olmalıdır.

Parametreler

ptr
IntPtr

nativeint

Yönetilmeyen bir bellek bloğunun işaretçisi.

structure
T

Verilerin kopyalandığı nesne.

Öznitelikler

Özel durumlar

Yapı düzeni sıralı veya açık değil.

Açıklamalar

PtrToStructure<T>(IntPtr, T) genellikle yapı parametreleri değer olarak IntPtr temsil edildiğinde COM birlikte çalışma ve platform çağırmada gereklidir. Bu yöntem aşırı yükleme değerini değer türleriyle kullanamazsınız. ptr Parametresi eşitse IntPtr.Zero ve T bir başvuru türüyse döndürülürnull. eşitse ptrIntPtr.Zero ve T bir değer türüyse, bir NullReferenceException oluşturulur.

Şunlara uygulanır