Marshal.Copy Méthode

Définition

Copie les données d'un tableau managé vers un pointeur mémoire non managé ou d'un pointeur mémoire non managé vers un tableau managé.

Surcharges

Copy(Single[], Int32, IntPtr, Int32)

Copie des données d'un tableau de nombres à virgule flottante simple précision managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(IntPtr, Single[], Int32, Int32)

Copie des données d'un pointeur mémoire non managé dans un tableau de nombres à virgule flottante simple précision managé.

Copy(IntPtr, IntPtr[], Int32, Int32)

Copie des données d’un pointeur mémoire non managé dans un tableau de IntPtr managé.

Copy(IntPtr, Int64[], Int32, Int32)

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 64 bits signés managé.

Copy(IntPtr, Int32[], Int32, Int32)

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 32 bits signés managé.

Copy(IntPtr, Int16[], Int32, Int32)

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 16 bits signés managé.

Copy(IntPtr, Double[], Int32, Int32)

Copie des données d'un pointeur mémoire non managé dans un tableau de nombres à virgule flottante double précision managé.

Copy(IntPtr[], Int32, IntPtr, Int32)

Copie des données d’un tableau de IntPtr managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(IntPtr, Byte[], Int32, Int32)

Copie des données d'un pointeur mémoire non managé dans un tableau d'entiers 8 bits non signés managé.

Copy(Int64[], Int32, IntPtr, Int32)

Copie des données à partir d'un tableau d'entiers 64 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(Int32[], Int32, IntPtr, Int32)

Copie des données à partir d'un tableau d'entiers 32 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(Int16[], Int32, IntPtr, Int32)

Copie des données à partir d'un tableau d'entiers 16 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(Double[], Int32, IntPtr, Int32)

Copie des données d'un tableau de nombres à virgule flottante double précision managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(Char[], Int32, IntPtr, Int32)

Copie des données d'un tableau de caractères managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(IntPtr, Char[], Int32, Int32)

Copie des données d'un pointeur mémoire non managé dans un tableau de caractères managé.

Copy(Byte[], Int32, IntPtr, Int32)

Copie des données d'un tableau d'entiers 8 bits non signés managé et unidimensionnel vers un pointeur mémoire non managé.

Copy(Single[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un tableau de nombres à virgule flottante simple précision managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(float[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(float[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Single[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

source, startIndex, destination ou length a la valeur null.

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Single[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un pointeur mémoire non managé dans un tableau de nombres à virgule flottante simple précision managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, float[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, float[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Single[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, IntPtr[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d’un pointeur mémoire non managé dans un tableau de IntPtr managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination

IntPtr[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Par conséquent, les données non managées qui correspondent au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler la Marshal.Copy méthode.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int64[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 64 bits signés managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, long[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, long[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Int64[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit lesource tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int32[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 32 bits signés managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, int[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, int[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Int32[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Int16[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un pointeur mémoire non managé vers un tableau d'entiers 16 bits signés managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, short[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, short[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Int16[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Double[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un pointeur mémoire non managé dans un tableau de nombres à virgule flottante double précision managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, double[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, double[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Double[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        double[] managedArray = { 0.1, 0.2, 0.3, 0.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.

            double[] managedArray2 = new double[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);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d’un tableau de IntPtr managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length);

Paramètres

source

IntPtr[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé IntPtr unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Byte[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un pointeur mémoire non managé dans un tableau d'entiers 8 bits non signés managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Byte[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int64[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un tableau d'entiers 64 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(long[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(long[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Int64[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

source, startIndex, destination ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int32[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un tableau d'entiers 32 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(int[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(int[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Int32[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

startIndex ou length est null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Int16[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données à partir d'un tableau d'entiers 16 bits signés managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(short[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(short[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Int16[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

source, startIndex, destination ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
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);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Double[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un tableau de nombres à virgule flottante double précision managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(double[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(double[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Double[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

source, startIndex, destination ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        double[] managedArray = { 0.1, 0.2, 0.3, 0.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.

            double[] managedArray2 = new double[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);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Char[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un tableau de caractères managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(char[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(char[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Char[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

startIndex, destination ou length est null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
// Remember that the actual size of System.Char in unmanaged memory is 2.
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        char[] managedArray = new char[1000];
        managedArray[0] = 'a';
        managedArray[1] = 'b';
        managedArray[2] = 'c';
        managedArray[3] = 'd';
        managedArray[999] = 'Z';

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

        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.

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

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
            Console.WriteLine("Here is the roundtripped array: {0} {1} {2} {3} {4}",
                               managedArray2[0], managedArray2[1], managedArray2[2], managedArray2[3],
                               managedArray2[999]);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(IntPtr, Char[], Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un pointeur mémoire non managé dans un tableau de caractères managé.

C#
[System.Security.SecurityCritical]
public static void Copy(IntPtr source, char[] destination, int startIndex, int length);
C#
public static void Copy(IntPtr source, char[] destination, int startIndex, int length);

Paramètres

source
IntPtr

Pointeur mémoire à partir duquel effectuer la copie.

destination
Char[]

Tableau dans lequel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau de destination où la copie doit commencer.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

source, destination, startIndex ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée, puis copie le tableau non managé dans la mémoire managée.

C#
// Remember that the actual size of System.Char in unmanaged memory is 2.
using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        char[] managedArray = new char[1000];
        managedArray[0] = 'a';
        managedArray[1] = 'b';
        managedArray[2] = 'c';
        managedArray[3] = 'd';
        managedArray[999] = 'Z';

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

        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.

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

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);
            Console.WriteLine("Here is the roundtripped array: {0} {1} {2} {3} {4}",
                               managedArray2[0], managedArray2[1], managedArray2[2], managedArray2[3],
                               managedArray2[999]);

            Console.WriteLine("The array was copied to unmanaged memory and back.");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}

Remarques

Les tableaux de style C non managés ne contiennent pas d’informations de limites, ce qui empêche la startIndex validation des paramètres et length . Ainsi, les données non managées correspondant au paramètre remplit le source tableau managé, quelle que soit son utilité. Vous devez initialiser le tableau managé avec la taille appropriée avant d’appeler cette méthode.

Voir aussi

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Copy(Byte[], Int32, IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copie des données d'un tableau d'entiers 8 bits non signés managé et unidimensionnel vers un pointeur mémoire non managé.

C#
[System.Security.SecurityCritical]
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length);
C#
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length);

Paramètres

source
Byte[]

Tableau unidimensionnel à partir duquel effectuer la copie.

startIndex
Int32

Index de base zéro dans le tableau source où la copie doit commencer.

destination
IntPtr

Pointeur mémoire de destination de la copie.

length
Int32

Nombre d'éléments de tableau à copier.

Attributs

Exceptions

startIndex et length ne sont pas valides.

source, startIndex, destination ou length a la valeur null.

Exemples

L’exemple suivant copie un tableau dans la mémoire non managée à l’aide de la Copy(Byte[], Int32, IntPtr, Int32) surcharge, puis copie le tableau non managé dans la mémoire managée à l’aide de la Copy(IntPtr, Byte[], Int32, Int32) surcharge.

C#
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);
        }
    }
}

Remarques

Vous pouvez utiliser cette méthode pour copier un sous-ensemble d’un tableau managé unidimensionnel vers un tableau de style C non managé.

S’applique à

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0