Marshal.Copy Metodo

Definizione

Copia i dati da una matrice gestita a un puntatore di memoria non gestita o viceversa.

Overload

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

Copia i dati da una matrice di numeri a virgola mobile a precisione singola gestita unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di numeri a virgola mobile a precisione singola gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice IntPtr gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 64 bit gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 32 bit gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 16 bit gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di numeri a virgola mobile a precisione doppia gestita.

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

Copia i dati da una matrice IntPtr unidimensionale gestita a un puntatore di memoria non gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi senza segno a 8 bit gestita.

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

Copia i dati da una matrice di interi con segno a 64 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da una matrice di interi con segno a 32 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da una matrice di interi con segno a 16 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da una matrice di numeri a virgola mobile a precisione doppia gestita e unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da una matrice di caratteri gestita e unidimensionale a un puntatore di memoria non gestita.

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

Copia i dati da un puntatore di memoria non gestita a una matrice di caratteri gestita.

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

Copia i dati da una matrice di interi senza segno a 8 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

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

Copia i dati da una matrice di numeri a virgola mobile a precisione singola gestita unidimensionale a un puntatore di memoria non gestita.

public:
 static void Copy(cli::array <float> ^ source, int startIndex, IntPtr destination, int length);
[System.Security.SecurityCritical]
public static void Copy (float[] source, int startIndex, IntPtr destination, int length);
public static void Copy (float[] source, int startIndex, IntPtr destination, int length);
[<System.Security.SecurityCritical>]
static member Copy : single[] * int * nativeint * int -> unit
static member Copy : single[] * int * nativeint * int -> unit
Public Shared Sub Copy (source As Single(), startIndex As Integer, destination As IntPtr, length As Integer)

Parametri

source
Single[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

source, startIndex, destination o length è null.

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di numeri a virgola mobile a precisione singola gestita.

public:
 static void Copy(IntPtr source, cli::array <float> ^ destination, int startIndex, int length);
[System.Security.SecurityCritical]
public static void Copy (IntPtr source, float[] destination, int startIndex, int length);
public static void Copy (IntPtr source, float[] destination, int startIndex, int length);
[<System.Security.SecurityCritical>]
static member Copy : nativeint * single[] * int * int -> unit
static member Copy : nativeint * single[] * int * int -> unit
Public Shared Sub Copy (source As IntPtr, destination As Single(), startIndex As Integer, length As Integer)

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Single[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice IntPtr gestita.

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

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination

IntPtr[]

nativeint[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti che corrispondono al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare il Marshal.Copy metodo.

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 64 bit gestita.

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

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Int64[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti alsource parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 32 bit gestita.

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

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Int32[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi con segno a 16 bit gestita.

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

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Int16[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di numeri a virgola mobile a precisione doppia gestita.

public:
 static void Copy(IntPtr source, cli::array <double> ^ destination, int startIndex, int length);
[System.Security.SecurityCritical]
public static void Copy (IntPtr source, double[] destination, int startIndex, int length);
public static void Copy (IntPtr source, double[] destination, int startIndex, int length);
[<System.Security.SecurityCritical>]
static member Copy : nativeint * double[] * int * int -> unit
static member Copy : nativeint * double[] * int * int -> unit
Public Shared Sub Copy (source As IntPtr, destination As Double(), startIndex As Integer, length As Integer)

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Double[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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);
        }
    }
}
Imports System.Runtime.InteropServices



Module Example


    Sub Main()
        ' Create a managed array.
        Dim managedArray As Double() = {0.1, 0.2, 0.3, 0.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 Double

            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

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da una matrice IntPtr unidimensionale gestita a un puntatore di memoria non gestita.

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

Parametri

source

IntPtr[]

nativeint[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita IntPtr .

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di interi senza segno a 8 bit gestita.

public:
 static void Copy(IntPtr source, cli::array <System::Byte> ^ destination, int startIndex, int length);
[System.Security.SecurityCritical]
public static void Copy (IntPtr source, byte[] destination, int startIndex, int length);
public static void Copy (IntPtr source, byte[] destination, int startIndex, int length);
[<System.Security.SecurityCritical>]
static member Copy : nativeint * byte[] * int * int -> unit
static member Copy : nativeint * byte[] * int * int -> unit
Public Shared Sub Copy (source As IntPtr, destination As Byte(), startIndex As Integer, length As Integer)

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Byte[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da una matrice di interi con segno a 64 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Parametri

source
Int64[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

source, startIndex, destination o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da una matrice di interi con segno a 32 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Parametri

source
Int32[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da una matrice di interi con segno a 16 bit gestita e unidimensionale a un puntatore di memoria non gestita.

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

Parametri

source
Int16[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

source, startIndex, destination o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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.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

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da una matrice di numeri a virgola mobile a precisione doppia gestita e unidimensionale a un puntatore di memoria non gestita.

public:
 static void Copy(cli::array <double> ^ source, int startIndex, IntPtr destination, int length);
[System.Security.SecurityCritical]
public static void Copy (double[] source, int startIndex, IntPtr destination, int length);
public static void Copy (double[] source, int startIndex, IntPtr destination, int length);
[<System.Security.SecurityCritical>]
static member Copy : double[] * int * nativeint * int -> unit
static member Copy : double[] * int * nativeint * int -> unit
Public Shared Sub Copy (source As Double(), startIndex As Integer, destination As IntPtr, length As Integer)

Parametri

source
Double[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

source, startIndex, destination o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

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);
        }
    }
}
Imports System.Runtime.InteropServices



Module Example


    Sub Main()
        ' Create a managed array.
        Dim managedArray As Double() = {0.1, 0.2, 0.3, 0.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 Double

            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

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da una matrice di caratteri gestita e unidimensionale a un puntatore di memoria non gestita.

public:
 static void Copy(cli::array <char> ^ source, int startIndex, IntPtr destination, int length);
[System.Security.SecurityCritical]
public static void Copy (char[] source, int startIndex, IntPtr destination, int length);
public static void Copy (char[] source, int startIndex, IntPtr destination, int length);
[<System.Security.SecurityCritical>]
static member Copy : char[] * int * nativeint * int -> unit
static member Copy : char[] * int * nativeint * int -> unit
Public Shared Sub Copy (source As Char(), startIndex As Integer, destination As IntPtr, length As Integer)

Parametri

source
Char[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

startIndex, destination o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

// 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);
        }
    }
}
' Remember that the actual size of System.Char in unmanaged memory is 2.

Imports System.Runtime.InteropServices



Module Module1

    Sub Main()
        ' Create a managed array.
        Dim managedArray As Char() = New Char(999) {}
        managedArray(0) = "a"c
        managedArray(1) = "b"c
        managedArray(2) = "c"c
        managedArray(3) = "d"c
        managedArray(999) = "Z"c

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

        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 As Char() = New Char(managedArray.Length - 1) {}

            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)

        End Try
    End Sub
End Module

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a

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

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

Copia i dati da un puntatore di memoria non gestita a una matrice di caratteri gestita.

public:
 static void Copy(IntPtr source, cli::array <char> ^ destination, int startIndex, int length);
[System.Security.SecurityCritical]
public static void Copy (IntPtr source, char[] destination, int startIndex, int length);
public static void Copy (IntPtr source, char[] destination, int startIndex, int length);
[<System.Security.SecurityCritical>]
static member Copy : nativeint * char[] * int * int -> unit
static member Copy : nativeint * char[] * int * int -> unit
Public Shared Sub Copy (source As IntPtr, destination As Char(), startIndex As Integer, length As Integer)

Parametri

source
IntPtr

nativeint

Puntatore di memoria da cui copiare.

destination
Char[]

Matrice in cui effettuare la copia.

startIndex
Int32

Indice in base zero nella matrice di destinazione in corrispondenza del quale inizia la copia.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

source, destination, startIndex o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita e quindi copia nuovamente la matrice non gestita in memoria gestita.

// 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);
        }
    }
}
' Remember that the actual size of System.Char in unmanaged memory is 2.

Imports System.Runtime.InteropServices



Module Module1

    Sub Main()
        ' Create a managed array.
        Dim managedArray As Char() = New Char(999) {}
        managedArray(0) = "a"c
        managedArray(1) = "b"c
        managedArray(2) = "c"c
        managedArray(3) = "d"c
        managedArray(999) = "Z"c

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

        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 As Char() = New Char(managedArray.Length - 1) {}

            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)

        End Try
    End Sub
End Module

Commenti

Le matrici in stile C non contengono informazioni sui limiti, che impediscono la convalida dei startIndex parametri e length . Pertanto, i dati non gestiti corrispondenti al source parametro popolano la matrice gestita indipendentemente dalla relativa utilità. È necessario inizializzare la matrice gestita con le dimensioni appropriate prima di chiamare questo metodo.

Vedi anche

Si applica a

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

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

Copia i dati da una matrice di interi senza segno a 8 bit gestita e unidimensionale a un puntatore di memoria non gestita.

public:
 static void Copy(cli::array <System::Byte> ^ source, int startIndex, IntPtr destination, int length);
[System.Security.SecurityCritical]
public static void Copy (byte[] source, int startIndex, IntPtr destination, int length);
public static void Copy (byte[] source, int startIndex, IntPtr destination, int length);
[<System.Security.SecurityCritical>]
static member Copy : byte[] * int * nativeint * int -> unit
static member Copy : byte[] * int * nativeint * int -> unit
Public Shared Sub Copy (source As Byte(), startIndex As Integer, destination As IntPtr, length As Integer)

Parametri

source
Byte[]

Matrice unidimensionale da cui copiare.

startIndex
Int32

Indice in base zero nella matrice di origine in corrispondenza del quale inizia la copia.

destination
IntPtr

nativeint

Puntatore di memoria in cui copiare.

length
Int32

Numero degli elementi di matrice da copiare.

Attributi

Eccezioni

startIndex e length non sono validi.

source, startIndex, destination o length è null.

Esempio

Nell'esempio seguente viene copiata una matrice in memoria non gestita usando l'overload Copy(Byte[], Int32, IntPtr, Int32) e quindi copia nuovamente la matrice non gestita nella memoria gestita usando l'overload Copy(IntPtr, Byte[], Int32, Int32) .

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.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

Commenti

È possibile usare questo metodo per copiare un subset di una matrice gestita unidimensionale in una matrice di stile C non gestita.

Si applica a