Marshal.Copy-Methode: (Int16[], Int32, IntPtr, Int32)

 

Veröffentlicht: Oktober 2016

Kopiert Daten aus einem eindimensionalen, verwalteten Array von 16-Bit-Ganzzahlen mit Vorzeichen in einen nicht verwalteten Speicherzeiger.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

Syntax

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

Parameter

  • source
    Type: System.Int16[]

    Das eindimensionale Array, aus dem kopiert werden soll.

  • startIndex
    Type: System.Int32

    Der nullbasierte Index im Quellarray, an dem der Kopiervorgang beginnen soll.

  • destination
    Type: System.IntPtr

    Der Speicherzeiger, in den kopiert werden soll.

  • length
    Type: System.Int32

    Die Anzahl der zu kopierenden Arrayelemente.

Ausnahmen

Exception Condition
ArgumentOutOfRangeException

startIndex und length sind nicht gültig.

ArgumentNullException

source, startIndex, destination oder length ist null.

Hinweise

Sie können diese Methode verwenden, eine Teilmenge von einem eindimensionalen, verwalteten Array in ein nicht verwaltetes Array von C-Format zu kopieren.

Beispiele

Im folgenden Beispiel wird ein Array auf nicht verwalteten Arbeitsspeicher kopiert und kopiert dann wieder in den verwalteten Speicher nicht verwaltete Array.

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

Sicherheit

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Versionsinformationen

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1

Siehe auch

Copy Überladen
Marshal-Klasse
System.Runtime.InteropServices-Namespace

Zurück zum Anfang