Compartilhar via


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

 

Publicado: agosto de 2016

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 um ponteiro de memória não gerenciada em uma matriz gerenciada de inteiro com sinal de 64 bits.

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

Sintaxe

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

Parâmetros

  • source
    Type: System.IntPtr

    O ponteiro de memória do qual copiar.

  • destination
    Type: System.Int64[]

    A matriz para a qual copiar.

  • startIndex
    Type: System.Int32

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

  • length
    Type: System.Int32

    O número de elementos da matriz a copiar.

Exceções

Exception Condition
ArgumentNullException

source, destination, startIndex ou length é null.

Comentários

Não gerenciados, matrizes de estilo C não contêm informações de limites, o que impede que o startIndex e length parâmetros sejam validados. Portanto, os dados não gerenciados correspondente asource parâmetro preenche a matriz gerenciada, independentemente de sua utilidade. Você deve inicializar a matriz gerenciada com o tamanho apropriado antes de chamar esse método.

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

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

            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
Copy Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices

Retornar ao início