Marshal.Copy Metoda

Definicja

Kopiuje dane z tablicy zarządzanej do niezarządzanego wskaźnika pamięci lub z niezarządzanego wskaźnika pamięci do tablicy zarządzanej.

Przeciążenia

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

Kopiuje dane z jednowymiarowej, zarządzanej tablicy liczb zmiennoprzecinkowych o pojedynczej precyzji do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy liczb zmiennoprzecinkowych o pojedynczej precyzji.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do tablicy zarządzanej IntPtr .

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej 64-bitowej tablicy całkowitej z podpisem.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy całkowitej z podpisem 32-bitowym.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy całkowitej z podpisem 16-bitowym.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy liczb zmiennoprzecinkowych o podwójnej precyzji.

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

Kopiuje dane z tablicy jednowymiarowej zarządzanej IntPtr do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej, niepodpisanej tablicy całkowitej bez znaku.

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

Kopiuje dane z jednowymiarowej, zarządzanej 64-bitowej tablicy całkowitej ze znakiem do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z jednowymiarowej tablicy całkowitej z podpisem 32-bitowym do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z jednowymiarowej, zarządzanej 16-bitowej tablicy całkowitej ze znakiem do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z jednowymiarowej tablicy liczb zmiennoprzecinkowych o podwójnej precyzji do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z jednowymiarowej tablicy znaków zarządzanych do niezarządzanego wskaźnika pamięci.

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

Kopiuje dane z niezarządzanego wskaźnika pamięci do tablicy znaków zarządzanych.

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

Kopiuje dane z jednowymiarowej, niepodpisanej tablicy całkowitej bez znaku do niezarządzanego wskaźnika pamięci.

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej tablicy liczb zmiennoprzecinkowych o pojedynczej precyzji do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Single[]

Tablica jednowymiarowa do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy źródłowej, w której należy rozpocząć kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

source, startIndex, destinationlub length to null.

Uwagi

Tej metody można użyć do skopiowania podzestawu tablicy zarządzanej jednowymiarowej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy liczb zmiennoprzecinkowych o pojedynczej precyzji.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Single[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające source parametrowi wypełnia tablicę zarządzaną niezależnie od jego użyteczności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do tablicy zarządzanej IntPtr .

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination

IntPtr[]

nativeint[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające source parametrowi wypełnia zarządzaną tablicę niezależnie od jego użyteczności. Przed wywołaniem metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze Marshal.Copy .

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej 64-bitowej tablicy całkowitej z podpisem.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Int64[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadającesource parametrowi wypełnia tablicę zarządzaną niezależnie od jego użyteczności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy całkowitej z podpisem 32-bitowym.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Int32[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające source parametrowi wypełnia tablicę zarządzaną niezależnie od jego użyteczności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy całkowitej z podpisem 16-bitowym.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Int16[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające source parametrowi wypełnia tablicę zarządzaną niezależnie od jego użyteczności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy liczb zmiennoprzecinkowych o podwójnej precyzji.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Double[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zera w tablicy docelowej, w której należy rozpocząć kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, destination, startIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Tablice niezarządzane w stylu C nie zawierają informacji o granicach, co uniemożliwia startIndex zweryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające parametrowi source wypełniają zarządzaną tablicę niezależnie od jego przydatności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej tablicy zarządzanej IntPtr do niezarządzanego wskaźnika pamięci.

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)

Parametry

source

IntPtr[]

nativeint[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, , destinationstartIndexlub length to null.

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej IntPtr do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy całkowitej bez znaku 8-bitowego.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Byte[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy docelowej, w której należy uruchomić kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, , destinationstartIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Niezarządzane tablice w stylu języka C nie zawierają informacji o granicach, co uniemożliwia startIndex weryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające parametrowi source wypełniają zarządzaną tablicę niezależnie od jego przydatności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej 64-bitowej tablicy całkowitej ze znakiem do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Int64[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

source, , startIndexdestinationlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej 32-bitowej tablicy całkowitej ze znakiem do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Int32[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

startIndex lub length ma wartość null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej 16-bitowej tablicy całkowitej ze znakiem do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Int16[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

source, , startIndexdestinationlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej tablicy liczb zmiennoprzecinkowych o podwójnej precyzji do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Double[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

source, , startIndexdestinationlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej tablicy znaków do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Char[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

startIndex, destinationlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z niezarządzanego wskaźnika pamięci do zarządzanej tablicy znaków.

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)

Parametry

source
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

destination
Char[]

Tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy docelowej, w której należy uruchomić kopiowanie.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

source, , destinationstartIndexlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do niezarządzanej pamięci, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej.

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

Uwagi

Niezarządzane tablice w stylu języka C nie zawierają informacji o granicach, co uniemożliwia startIndex weryfikowanie parametrów i length . W związku z tym niezarządzane dane odpowiadające parametrowi source wypełniają zarządzaną tablicę niezależnie od jego przydatności. Przed wywołaniem tej metody należy zainicjować tablicę zarządzaną o odpowiednim rozmiarze.

Zobacz też

Dotyczy

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

Źródło:
Marshal.cs
Źródło:
Marshal.cs
Źródło:
Marshal.cs

Kopiuje dane z jednowymiarowej, zarządzanej 8-bitowej tablicy całkowitej bez znaku do niezarządzanego wskaźnika pamięci.

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)

Parametry

source
Byte[]

Jednowymiarowa tablica do skopiowania.

startIndex
Int32

Indeks oparty na zerze w tablicy źródłowej, w której należy uruchomić kopiowanie.

destination
IntPtr

nativeint

Wskaźnik pamięci do skopiowania.

length
Int32

Liczba elementów tablicy do skopiowania.

Atrybuty

Wyjątki

startIndex i length są nieprawidłowe.

source, , startIndexdestinationlub length to null.

Przykłady

Poniższy przykład kopiuje tablicę do pamięci niezarządzanej przy użyciu Copy(Byte[], Int32, IntPtr, Int32) przeciążenia, a następnie kopiuje niezarządzaną tablicę z powrotem do pamięci zarządzanej Copy(IntPtr, Byte[], Int32, Int32) przy użyciu przeciążenia.

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

Uwagi

Za pomocą tej metody można skopiować podzbiór jednowymiarowej tablicy zarządzanej do niezarządzanej tablicy w stylu C.

Dotyczy