英語で読む

次の方法で共有


Marshal.Copy メソッド

定義

マネージド配列からアンマネージド メモリ ポインター、またはアンマネージド メモリ ポインターからマネージド配列にデータをコピーします。

オーバーロード

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

1 次元の単精度浮動小数点数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

アンマネージド メモリ ポインターのデータを単精度浮動小数点数のマネージド配列にコピーします。

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

アンマネージド メモリ ポインターのデータをマネージド IntPtr 配列にコピーします。

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

アンマネージド メモリ ポインターのデータを 64 ビット符号付き整数のマネージド配列にコピーします。

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

アンマネージド メモリ ポインターのデータを 32 ビット符号付き整数のマネージド配列にコピーします。

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

アンマネージド メモリ ポインターのデータを 16 ビット符号付き整数のマネージド配列にコピーします。

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

アンマネージド メモリ ポインターのデータを倍精度浮動小数点数のマネージド配列にコピーします。

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

1 次元の IntPtr マネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

アンマネージド メモリ ポインターのデータを 8 ビット符号なし整数のマネージド配列にコピーします。

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

1 次元の 64 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

1 次元の 32 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

1 次元の 16 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

1 次元の倍精度浮動小数点数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

1 次元の文字列のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

アンマネージド メモリ ポインターのデータを文字列のマネージド配列にコピーします。

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

1 次元の 8 ビット符号なし整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の単精度浮動小数点数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

sourcestartIndexdestination、または lengthnull です。

注釈

このメソッドを使用すると、1 次元マネージド配列のサブセットをアンマネージド C スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを単精度浮動小数点数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Single[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

注釈

アンマネージ C スタイルの配列には境界情報が含まれていないため、 startIndex パラメーターと length パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータをマネージド 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)

パラメーター

source
IntPtr

nativeint

コピー元のメモリ ポインター。

destination

IntPtr[]

nativeint[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

注釈

アンマネージ C スタイルの配列には境界情報が含まれていないため、 startIndex パラメーターと length パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは、その有用性に source 関係なくマネージド配列にデータを設定します。 メソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する Marshal.Copy 必要があります。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを 64 ビット符号付き整数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Int64[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データはsource 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを 32 ビット符号付き整数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Int32[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを 16 ビット符号付き整数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Int16[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを倍精度浮動小数点数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Double[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の IntPtr マネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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[]

nativeint[]

コピー元の 1 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

注釈

このメソッドを使用すると、1 次元マネージド IntPtr 配列のサブセットをアンマネージド C スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを 8 ビット符号なし整数のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Byte[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の 64 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

sourcestartIndexdestination、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の 32 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

startIndex または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の 16 ビット符号付き整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

sourcestartIndexdestination、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の倍精度浮動小数点数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

sourcestartIndexdestination、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

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 スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の文字列のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

startIndexdestination、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

// 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 スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージド メモリ ポインターのデータを文字列のマネージド配列にコピーします。

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

nativeint

コピー元のメモリ ポインター。

destination
Char[]

コピー先の配列。

startIndex
Int32

コピーの開始位置となる、コピー先の配列内の 0 から始まるインデックス。

length
Int32

コピーする配列要素の数。

属性

例外

sourcedestinationstartIndex、または lengthnull です。

次の例では、アンマネージド メモリに配列をコピーし、アンマネージ配列をマネージド メモリにコピーします。

// 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 パラメーターが検証されません。 したがって、 パラメーターに対応するアンマネージド データは source 、その有用性に関係なくマネージド配列に入力されます。 このメソッドを呼び出す前に、適切なサイズでマネージド配列を初期化する必要があります。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

1 次元の 8 ビット符号なし整数のマネージド配列のデータをアンマネージド メモリ ポインターにコピーします。

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 次元配列。

startIndex
Int32

コピーの開始位置となる、コピー元の配列内の 0 から始まるインデックス。

destination
IntPtr

nativeint

コピー先のメモリ ポインター。

length
Int32

コピーする配列要素の数。

属性

例外

startIndex および length が有効ではありません。

sourcestartIndexdestination、または lengthnull です。

次の例では、 オーバーロードを使用してアンマネージ メモリに配列を Copy(Byte[], Int32, IntPtr, Int32) コピーし、 オーバーロードを使用 Copy(IntPtr, Byte[], Int32, Int32) してアンマネージ配列をマネージド メモリにコピーします。

using System;
using System.Runtime.InteropServices;

class Example
{

    static void Main()
    {
        // Create a managed array.
        byte[] managedArray = { 1, 2, 3, 4 };

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

        IntPtr pnt = Marshal.AllocHGlobal(size);

        try
        {
            // Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length);

            // Copy the unmanaged array back to another managed array.

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

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length);

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



Module Example


    Sub Main()
        ' Create a managed array.
        Dim managedArray As Byte() = {1, 2, 3, 4}

        ' Initialize unmanaged memory to hold the array.
        Dim size As Integer = Marshal.SizeOf(managedArray(0)) * managedArray.Length

        Dim pnt As IntPtr = Marshal.AllocHGlobal(size)

        Try
            ' Copy the array to unmanaged memory.
            Marshal.Copy(managedArray, 0, pnt, managedArray.Length)

            ' Copy the unmanaged array back to another managed array.
            Dim managedArray2(managedArray.Length) As Byte

            Marshal.Copy(pnt, managedArray2, 0, managedArray.Length)

            Console.WriteLine("The array was copied to unmanaged memory and back.")

        Finally
            ' Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt)
        End Try

    End Sub
End Module

注釈

このメソッドを使用すると、1 次元マネージド配列のサブセットをアンマネージド C スタイルの配列にコピーできます。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0