Compartilhar via


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

 

Dica

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Copia os dados de uma matriz unidimensional de inteiro com sinal de 32 bits para um ponteiro de memória não gerenciada.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (em mscorlib.dll)

Sintaxe

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

Parâmetros

  • source
    Type: System.Int32[]

    A matriz unidimensional a ser copiada.

  • startIndex
    Type: System.Int32

    O índice baseado em zero na matriz de origem em que a cópia deve iniciar.

  • destination
    Type: System.IntPtr

    O ponteiro de memória para o qual copiar.

  • length
    Type: System.Int32

    O número de elementos da matriz a copiar.

Exceções

Exception Condition
ArgumentOutOfRangeException

startIndex e length não são válidos.

ArgumentNullException

startIndex ou length é null.

Comentários

Você pode usar esse método para copiar um subconjunto de uma matriz unidimensional de gerenciado para uma matriz de estilo C não gerenciada.

Exemplos

O exemplo a seguir copia uma matriz para a memória não gerenciada e, em seguida, copia a matriz não gerenciada para memória gerenciada.

using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        int[] 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.

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

            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

Segurança

SecurityCriticalAttribute

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

Informações de Versão

Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Silverlight
Disponível desde 2.0
Windows Phone Silverlight
Disponível desde 7.0
Windows Phone
Disponível desde 8.1

Confira Também

Copy Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices

Retornar ao início