Marshal.Copy 메서드
정의
관리되는 배열에서 관리되지 않는 메모리 포인터로 데이터를 복사하거나 관리되지 않는 메모리 포인터에서 관리되는 배열로 데이터를 복사합니다.Copies data from a managed array to an unmanaged memory pointer, or from an unmanaged memory pointer to a managed array.
오버로드
Copy(Single[], Int32, IntPtr, Int32) |
관리되는 1차원 단정밀도 부동 소수점 수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed single-precision floating-point number array to an unmanaged memory pointer. |
Copy(IntPtr[], Int32, IntPtr, Int32) |
관리되는 IntPtr 배열의 데이터를 관리되지 않는 메모리 포인터에 복사합니다.Copies data from a one-dimensional, managed IntPtr array to an unmanaged memory pointer. |
Copy(IntPtr, Single[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 단정밀도 부동 소수점 수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array. |
Copy(IntPtr, Int64[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 64비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array. |
Copy(IntPtr, Int32[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 32비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array. |
Copy(IntPtr, Int16[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 16비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array. |
Copy(IntPtr, Double[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 배정밀도 부동 소수점 수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array. |
Copy(IntPtr, IntPtr[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 IntPtr 배열에 복사합니다.Copies data from an unmanaged memory pointer to a managed IntPtr array. |
Copy(IntPtr, Byte[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 8비트 부호 없는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array. |
Copy(Int64[], Int32, IntPtr, Int32) |
관리되는 64비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer. |
Copy(Int32[], Int32, IntPtr, Int32) |
관리되는 32비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer. |
Copy(Int16[], Int32, IntPtr, Int32) |
관리되는 16비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer. |
Copy(IntPtr, Char[], Int32, Int32) |
관리되지 않는 메모리 포인터의 데이터를 관리되는 문자 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed character array. |
Copy(Double[], Int32, IntPtr, Int32) |
관리되는 1차원 배정밀도 부동 소수점 수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed double-precision floating-point number array to an unmanaged memory pointer. |
Copy(Char[], Int32, IntPtr, Int32) |
관리되는 1차원 문자 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed character array to an unmanaged memory pointer. |
Copy(Byte[], Int32, IntPtr, Int32) |
관리되는 8비트 부호 없는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer. |
Copy(Single[], Int32, IntPtr, Int32)
관리되는 1차원 단정밀도 부동 소수점 수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed single-precision floating-point number array to an unmanaged memory pointer.
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)
매개 변수
- source
- Single[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
source
, startIndex
, destination
또는 length
가 null
입니다.source
, startIndex
, destination
, or length
is null
.
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(IntPtr[], Int32, IntPtr, Int32)
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)
매개 변수
- source
- IntPtr[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 IntPtr 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed IntPtr array to an unmanaged C-style array.
적용 대상
Copy(IntPtr, Single[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 단정밀도 부동 소수점 수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Single[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(IntPtr, Int64[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 64비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Int64[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to thesource
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(IntPtr, Int32[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 32비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Int32[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(IntPtr, Int16[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 16비트 부호 있는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Int16[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(IntPtr, Double[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 배정밀도 부동 소수점 수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Double[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(IntPtr, IntPtr[], Int32, Int32)
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- IntPtr[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Therefore, the unmanaged data that corresponds to the source
parameter populates the managed array regardless of its usefulness. 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다 Marshal.Copy .You must initialize the managed array with the appropriate size before calling the Marshal.Copy method.
적용 대상
Copy(IntPtr, Byte[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 8비트 부호 없는 정수 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Byte[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(Int64[], Int32, IntPtr, Int32)
관리되는 64비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer.
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)
매개 변수
- source
- Int64[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
source
, startIndex
, destination
또는 length
가 null
입니다.source
, startIndex
, destination
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(Int32[], Int32, IntPtr, Int32)
관리되는 32비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer.
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)
매개 변수
- source
- Int32[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
startIndex
또는 length
가 null
인 경우startIndex
or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(Int16[], Int32, IntPtr, Int32)
관리되는 16비트 부호 있는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer.
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)
매개 변수
- source
- Int16[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
source
, startIndex
, destination
또는 length
가 null
입니다.source
, startIndex
, destination
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(IntPtr, Char[], Int32, Int32)
관리되지 않는 메모리 포인터의 데이터를 관리되는 문자 배열로 복사합니다.Copies data from an unmanaged memory pointer to a managed character array.
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)
매개 변수
- source
- IntPtr
복사할 메모리 포인터입니다.The memory pointer to copy from.
- destination
- Char[]
복사할 대상 배열입니다.The array to copy to.
- startIndex
- Int32
대상 배열에서 복사를 시작해야 할 0부터 시작하는 인덱스입니다.The zero-based index in the destination array where copying should start.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
source
, destination
, startIndex
또는 length
가 null
입니다.source
, destination
, startIndex
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
// 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
설명
관리 되지 않는 C 스타일 배열에는 startIndex
및 매개 변수의 유효성을 검사 하지 못하도록 하는 범위 정보가 포함 되어 있지 않습니다 length
.Unmanaged, C-style arrays do not contain bounds information, which prevents the startIndex
and length
parameters from being validated. 따라서 매개 변수에 해당 하는 관리 되지 않는 데이터는 source
유용성에 관계 없이 관리 되는 배열을 채웁니다.Thus, the unmanaged data corresponding to the source
parameter populates the managed array regardless of its usefulness. 이 메서드를 호출 하기 전에 적절 한 크기를 사용 하 여 관리 되는 배열을 초기화 해야 합니다.You must initialize the managed array with the appropriate size before calling this method.
추가 정보
적용 대상
Copy(Double[], Int32, IntPtr, Int32)
관리되는 1차원 배정밀도 부동 소수점 수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed double-precision floating-point number array to an unmanaged memory pointer.
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)
매개 변수
- source
- Double[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
source
, startIndex
, destination
또는 length
가 null
입니다.source
, startIndex
, destination
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(Char[], Int32, IntPtr, Int32)
관리되는 1차원 문자 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed character array to an unmanaged memory pointer.
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)
매개 변수
- source
- Char[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
startIndex
, destination
또는 length
가 null
입니다.startIndex
, destination
, or length
is null
.
예제
다음 예제에서는 관리 되지 않는 메모리에 배열을 복사 하 고 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다.The following example copies an array to unmanaged memory and then copies the unmanaged array back to managed memory.
// 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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.
적용 대상
Copy(Byte[], Int32, IntPtr, Int32)
관리되는 8비트 부호 없는 1차원 정수 배열의 데이터를 관리되지 않는 메모리 포인터로 복사합니다.Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer.
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)
매개 변수
- source
- Byte[]
복사할 1차원 배열입니다.The one-dimensional array to copy from.
- startIndex
- Int32
소스 배열에서 복사를 시작해야 할 인덱스(0부터 시작)입니다.The zero-based index in the source array where copying should start.
- destination
- IntPtr
복사할 대상 메모리 포인터입니다.The memory pointer to copy to.
- length
- Int32
복사할 배열 요소 수입니다.The number of array elements to copy.
- 특성
예외
startIndex
및 length
가 잘못되었습니다.startIndex
and length
are not valid.
source
, startIndex
, destination
또는 length
가 null
입니다.source
, startIndex
, destination
, or length
is null
.
예제
다음 예제에서는 오버 로드를 사용 하 여 관리 되지 않는 메모리에 배열을 복사 하 Copy(Byte[], Int32, IntPtr, Int32) 고 오버 로드를 사용 하 여 관리 되지 않는 배열을 관리 되는 메모리로 다시 복사 합니다 Copy(IntPtr, Byte[], Int32, Int32) .The following example copies an array to unmanaged memory by using the Copy(Byte[], Int32, IntPtr, Int32) overload, and then copies the unmanaged array back to managed memory by using the Copy(IntPtr, Byte[], Int32, Int32) overload.
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
설명
이 메서드를 사용 하 여 1 차원 관리 되는 배열의 하위 집합을 관리 되지 않는 C 스타일 배열에 복사할 수 있습니다.You can use this method to copy a subset of a one-dimensional managed array to an unmanaged C-style array.