Marshal.ReadInt16 Metodo

Definizione

Legge un valore intero con segno a 16 bit dalla memoria non gestita. La lettura da posizioni di memoria non allineate è supportata.

Overload

ReadInt16(IntPtr)

Legge un valore intero con segno a 16 bit dalla memoria non gestita.

ReadInt16(IntPtr, Int32)

Legge un intero con segno a 16 bit a un offset specificato dalla memoria non gestita.

ReadInt16(Object, Int32)
Obsoleti.

Legge un intero con segno a 16 bit a un offset specificato dalla memoria non gestita.

ReadInt16(IntPtr)

Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs

Legge un valore intero con segno a 16 bit dalla memoria non gestita.

public:
 static short ReadInt16(IntPtr ptr);
[System.Security.SecurityCritical]
public static short ReadInt16 (IntPtr ptr);
public static short ReadInt16 (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadInt16 : nativeint -> int16
static member ReadInt16 : nativeint -> int16
Public Shared Function ReadInt16 (ptr As IntPtr) As Short

Parametri

ptr
IntPtr

nativeint

Indirizzo nella memoria non gestita da cui leggere.

Restituisce

Intero con segno a 16 bit letto dalla memoria non gestita.

Attributi

Eccezioni

ptr non è un formato riconosciuto.

-oppure-

ptr è null.

-oppure-

ptr non è valido.

Esempio

Nell'esempio seguente viene illustrato come leggere e scrivere in una matrice non gestita usando i ReadInt16 metodi e WriteInt16 .

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    Dim unmanagedArray As IntPtr = Marshal.AllocHGlobal(10 * elementSize)

    ' Set the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Marshal.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    Next i
    Console.WriteLine("Unmanaged memory written.")

    Console.WriteLine("Reading unmanaged memory:")
    ' Print the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

Nell'esempio seguente viene illustrato come utilizzare il ReadInt16 metodo per leggere il valore di una variabile non gestita short .


using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged short.
    short myShort = 42;

    // Read the short as a managed Int16.
        Int16 ^ myManagedVal = Marshal::ReadInt16((IntPtr) &myShort);

    // Display the value to the console.
    Console::WriteLine(myManagedVal);
}

Commenti

ReadInt16 ha un offset implicito pari a 0. Questo metodo consente l'interazione diretta con una matrice di tipo Int16 C non gestita, eliminando i costi di copia di un'intera matrice non gestita (usando Marshal.Copy) in una matrice gestita separata prima di leggere i valori degli elementi.

La lettura da posizioni di memoria non allineate è supportata.

Vedi anche

Si applica a

ReadInt16(IntPtr, Int32)

Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs

Legge un intero con segno a 16 bit a un offset specificato dalla memoria non gestita.

public:
 static short ReadInt16(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static short ReadInt16 (IntPtr ptr, int ofs);
public static short ReadInt16 (IntPtr ptr, int ofs);
[<System.Security.SecurityCritical>]
static member ReadInt16 : nativeint * int -> int16
static member ReadInt16 : nativeint * int -> int16
Public Shared Function ReadInt16 (ptr As IntPtr, ofs As Integer) As Short

Parametri

ptr
IntPtr

nativeint

Indirizzo di base nella memoria non gestita da cui leggere.

ofs
Int32

Offset di byte supplementare, aggiunto al parametro ptr prima della lettura.

Restituisce

Intero con segno a 16 bit letto dalla memoria non gestita a un offset specificato.

Attributi

Eccezioni

L'indirizzo di base (ptr) più il byte di offset (ofs) produce un indirizzo Null o non valido.

Esempio

Nell'esempio seguente viene illustrato come leggere e scrivere in una matrice non gestita usando i ReadInt16 metodi e WriteInt16 .

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    Dim unmanagedArray As IntPtr = Marshal.AllocHGlobal(10 * elementSize)

    ' Set the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Marshal.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    Next i
    Console.WriteLine("Unmanaged memory written.")

    Console.WriteLine("Reading unmanaged memory:")
    ' Print the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Console.WriteLine(Marshal.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

Nell'esempio seguente viene illustrato come utilizzare il ReadInt16 metodo per leggere il valore di una variabile non gestita short .


using namespace System;
using namespace System::Runtime::InteropServices;

void main()
{
    // Create an unmanaged short pointer.
    short * myShort;
    short tmp = 42;
    // Initialize it to another value.
    myShort = &tmp;

    // Read value as a managed Int16.
    Int16 ^ myManagedVal = Marshal::ReadInt16((IntPtr) myShort, 0);

    // Display the value to the console.
    Console::WriteLine(myManagedVal);
}

Commenti

ReadInt16 consente l'interazione diretta con una matrice con segno a 16 bit non gestita, eliminando il costo di copiare un'intera matrice non gestita (usando Marshal.Copy) in una matrice gestita separata prima di leggere i valori degli elementi.

La lettura da posizioni di memoria non allineate è supportata.

Vedi anche

Si applica a

ReadInt16(Object, Int32)

Origine:
Marshal.CoreCLR.cs
Origine:
Marshal.CoreCLR.cs
Origine:
Marshal.CoreCLR.cs

Attenzione

ReadInt16(Object, Int32) may be unavailable in future releases.

Legge un intero con segno a 16 bit a un offset specificato dalla memoria non gestita.

public:
 static short ReadInt16(System::Object ^ ptr, int ofs);
[System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static short ReadInt16 (object ptr, int ofs);
[System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")]
public static short ReadInt16 (object ptr, int ofs);
public static short ReadInt16 (object ptr, int ofs);
[System.Security.SecurityCritical]
public static short ReadInt16 (object ptr, int ofs);
[<System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member ReadInt16 : obj * int -> int16
[<System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")>]
static member ReadInt16 : obj * int -> int16
static member ReadInt16 : obj * int -> int16
[<System.Security.SecurityCritical>]
static member ReadInt16 : obj * int -> int16
Public Shared Function ReadInt16 (ptr As Object, ofs As Integer) As Short

Parametri

ptr
Object

Indirizzo di base dell'oggetto di origine nella memoria non gestita.

ofs
Int32

Offset di byte supplementare, aggiunto al parametro ptr prima della lettura.

Restituisce

Intero con segno a 16 bit letto dalla memoria non gestita a un offset specificato.

Attributi

Eccezioni

L'indirizzo di base (ptr) più il byte di offset (ofs) produce un indirizzo Null o non valido.

ptr è un oggetto ArrayWithOffset. Questo metodo non accetta parametri ArrayWithOffset.

Commenti

ReadInt16 consente l'interazione diretta con una matrice con segno a 16 bit non gestita, eliminando il costo di copiare un'intera matrice non gestita (usando Marshal.Copy) in una matrice gestita separata prima di leggere i valori degli elementi.

La lettura da posizioni di memoria non allineate è supportata.

Vedi anche

Si applica a