Compartir a través de


Método Marshal.Copy (IntPtr, Byte[], Int32, Int32)

 

Publicado: octubre de 2016

Copia datos de un puntero de memoria no administrada a una matriz administrada de enteros de 8 bits sin signo.

Espacio de nombres:   System.Runtime.InteropServices
Ensamblado:  mscorlib (en mscorlib.dll)

Sintaxis

[SecurityCriticalAttribute]
public static void Copy(
    IntPtr source,
    byte[] destination,
    int startIndex,
    int length
)
public:
[SecurityCriticalAttribute]
static void Copy(
    IntPtr source,
    array<unsigned char>^ destination,
    int startIndex,
    int length
)
[<SecurityCriticalAttribute>]
static member Copy : 
        source:nativeint *
        destination:byte[] *
        startIndex:int *
        length:int -> unit
<SecurityCriticalAttribute>
Public Shared Sub Copy (
    source As IntPtr,
    destination As Byte(),
    startIndex As Integer,
    length As Integer
)

Parámetros

  • source
    Type: System.IntPtr

    Puntero de memoria del que se va a copiar.

  • destination
    Type: System.Byte[]

    Matriz en la que se va a copiar.

  • startIndex
    Type: System.Int32

    Índice de base cero de la matriz de destino donde debe comenzar la copia.

  • length
    Type: System.Int32

    Número de elementos de la matriz que se van a copiar.

Excepciones

Exception Condition
ArgumentNullException

source, destination, startIndex o length es null.

Comentarios

No administradas, matrices de estilo C no contienen información de límites, lo que evita la startIndex y length parámetros desde que se está validando. Por lo tanto, los datos no administrados correspondientes a la source parámetro rellena la matriz administrada independientemente de su utilidad. Debe inicializar la matriz administrada con el tamaño adecuado antes de llamar a este método.

Ejemplos

En el ejemplo siguiente se copia una matriz en la memoria no administrada y, a continuación, se copia la matriz no administrada a la memoria administrada.

using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        byte[] managedArray = { 1, 2, 3, 4 };

        // Initialize unmanaged memory to hold the array.
        int size = Marshal.SizeOf(managedArray[0]) * managedArray.Length;

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

            byte[] managedArray2 = new byte[managedArray.Length];

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

            Console.WriteLine("The array was copied to unmanaged memory and back.");

        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }



    }

}
Imports System
Imports System.Runtime.InteropServices



Module Example


    Sub Main()
        ' Create a managed array.
        Dim managedArray As Byte() = {1, 2, 3, 4}

        ' Initialize unmanaged memory to hold the array.
        Dim size As Integer = Marshal.SizeOf(managedArray(0)) * managedArray.Length

        Dim pnt As IntPtr = Marshal.AllocHGlobal(size)

        Try
            ' Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length)

            ' Copy the unmanaged array back to another managed array.
            Dim managedArray2(managedArray.Length) As Byte

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length)

            Console.WriteLine("The array was copied to unmanaged memory and back.")

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

    End Sub
End Module

Seguridad

SecurityCriticalAttribute

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

Información de versión

Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 1.1
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1

Ver también

Copy
Copy Sobrecarga
Clase Marshal
Espacio de nombres System.Runtime.InteropServices

Volver al principio