Marshal.Copy Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Copia dados de um array gerido para um ponteiro de memória não gerido, ou de um ponteiro de memória não gerido para um array gerido.
Sobrecargas
| Name | Description |
|---|---|
| Copy(Single[], Int32, IntPtr, Int32) |
Copia dados de um array de números de ponto flutuante unidimensional, gerido e de precisão simples para um ponteiro de memória não gerido. |
| Copy(IntPtr, Single[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array de números de ponto flutuante de precisão simples gerida. |
| Copy(IntPtr, IntPtr[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array gerido IntPtr . |
| Copy(IntPtr, Int64[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 64 bits com sinal. |
| Copy(IntPtr, Int32[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 32 bits com sinal. |
| Copy(IntPtr, Int16[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 16 bits com sinal. |
| Copy(IntPtr, Double[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerido para um array de números de ponto flutuante de dupla precisão gerido. |
| Copy(IntPtr[], Int32, IntPtr, Int32) |
Copia dados de um array unidimensional e gerido IntPtr para um ponteiro de memória não gerido. |
| Copy(IntPtr, Byte[], Int32, Int32) |
Copia dados de um ponteiro de memória não gerida para um array inteiro não assinado de 8 bits gerido. |
| Copy(Int64[], Int32, IntPtr, Int32) |
Copia dados de um array inteiro unidimensional, gerido e gerido de 64 bits para um ponteiro de memória não gerido. |
| Copy(Int32[], Int32, IntPtr, Int32) |
Copia dados de um array inteiro unidimensional, gerido e gerido de 32 bits, para um ponteiro de memória não gerido. |
| Copy(Int16[], Int32, IntPtr, Int32) |
Copia dados de um array inteiro unidimensional, gerido e gerido de 16 bits para um ponteiro de memória não gerido. |
| Copy(Double[], Int32, IntPtr, Int32) |
Copia dados de um array de números de ponto flutuante de dupla precisão unidimensional e gerido para um ponteiro de memória não gerido. |
| Copy(Char[], Int32, IntPtr, Int32) |
Copia dados de um array de caracteres unidimensional e gerido para um ponteiro de memória não gerido. |
| Copy(IntPtr, Char[], Int32, Int32) |
Copia dados de um apontador de memória não gerida para um array de caracteres gerido. |
| Copy(Byte[], Int32, IntPtr, Int32) |
Copia dados de um array inteiro unidimensional, gerido de 8 bits, sem sinal, para um ponteiro de memória não gerido. |
Copy(Single[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array de números de ponto flutuante unidimensional, gerido e de precisão simples para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Single[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
source, startIndex, destination, ou length é null.
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(IntPtr, Single[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array de números de ponto flutuante de precisão simples gerida.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Single[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(IntPtr, IntPtr[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array gerido 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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
-
IntPtr[]
nativeint[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos que correspondem ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar o Marshal.Copy método.
Aplica-se a
Copy(IntPtr, Int64[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 64 bits com sinal.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Int64[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes aosource parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(IntPtr, Int32[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 32 bits com sinal.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Int32[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(IntPtr, Int16[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array inteiro gestual de 16 bits com sinal.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Int16[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(IntPtr, Double[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerido para um array de números de ponto flutuante de dupla precisão gerido.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Double[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(IntPtr[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array unidimensional e gerido IntPtr para um ponteiro de memória não gerido.
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)
Parâmetros
- source
-
IntPtr[]
nativeint[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Observações
Pode usar este método para copiar um subconjunto de um array gerido IntPtr unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(IntPtr, Byte[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um ponteiro de memória não gerida para um array inteiro não assinado de 8 bits gerido.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Byte[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(Int64[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array inteiro unidimensional, gerido e gerido de 64 bits para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Int64[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
source, startIndex, destination, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(Int32[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array inteiro unidimensional, gerido e gerido de 32 bits, para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Int32[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
startIndex ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(Int16[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array inteiro unidimensional, gerido e gerido de 16 bits para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Int16[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
source, startIndex, destination, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(Double[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array de números de ponto flutuante de dupla precisão unidimensional e gerido para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Double[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
source, startIndex, destination, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(Char[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array de caracteres unidimensional e gerido para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Char[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
startIndex, destination, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
// 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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.
Aplica-se a
Copy(IntPtr, Char[], Int32, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um apontador de memória não gerida para um array de caracteres gerido.
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)
Parâmetros
- source
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- destination
- Char[]
O array para copiar.
- startIndex
- Int32
O índice baseado em zero no array de destino onde a cópia deve começar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
source, destination, startIndex, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida e depois copia o array não gerido de volta para memória gerida.
// 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
Observações
Os arrays não geridos, no estilo C, não contêm informação de limites, o que impede que os startIndex parâmetros e length sejam validados. Assim, os dados não geridos correspondentes ao source parâmetro preenchem o array gerido independentemente da sua utilidade. Deve inicializar o array gerido com o tamanho adequado antes de chamar este método.
Ver também
Aplica-se a
Copy(Byte[], Int32, IntPtr, Int32)
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
- Origem:
- Marshal.cs
Copia dados de um array inteiro unidimensional, gerido de 8 bits, sem sinal, para um ponteiro de memória não gerido.
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)
Parâmetros
- source
- Byte[]
O array unidimensional para copiar.
- startIndex
- Int32
O índice baseado em zero no array de origem onde a cópia deve começar.
- destination
-
IntPtr
nativeint
O ponteiro de memória para copiar.
- length
- Int32
O número de elementos do array a copiar.
- Atributos
Exceções
startIndex e length não são válidas.
source, startIndex, destination, ou length é null.
Exemplos
O exemplo seguinte copia um array para memória não gerida usando a Copy(Byte[], Int32, IntPtr, Int32) sobrecarga, e depois copia o array não gerido de volta para a memória gerida usando a Copy(IntPtr, Byte[], Int32, Int32) sobrecarga.
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
Observações
Pode usar este método para copiar um subconjunto de um array gerido unidimensional para um array não gerido ao estilo C.