Compartir a través de


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

 

Publicado: octubre de 2016

Copia datos de una matriz unidimensional administrada de enteros de 16 bits con signo a un puntero de memoria no administrada.

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

Sintaxis

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

Parámetros

  • source
    Type: System.Int16[]

    Matriz unidimensional de la que se va a copiar.

  • startIndex
    Type: System.Int32

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

  • destination
    Type: System.IntPtr

    Puntero de memoria en el que se va a copiar.

  • length
    Type: System.Int32

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

Excepciones

Exception Condition
ArgumentOutOfRangeException

startIndex y length no son válidos.

ArgumentNullException

source, startIndex, destination o length es null.

Comentarios

Puede usar este método para copiar un subconjunto de una matriz unidimensional administrada a una matriz de estilo C no administrada.

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.
        short[] 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.

            short[] managedArray2 = new short[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 Short() = {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 Short

            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 Sobrecarga
Clase Marshal
Espacio de nombres System.Runtime.InteropServices

Volver al principio