Marshal.ReadInt16 Método

Definição

Lê um inteiro com sinal de 16 bits da memória não gerenciada. Há suporte para leitura de locais de memória não assinados.

Sobrecargas

Nome Description
ReadInt16(IntPtr)

Lê um inteiro com sinal de 16 bits da memória não gerenciada.

ReadInt16(IntPtr, Int32)

Lê um inteiro com sinal de 16 bits em um determinado deslocamento da memória não gerenciada.

ReadInt16(Object, Int32)
Obsoleto.

Lê um inteiro com sinal de 16 bits em um determinado deslocamento da memória não gerenciada.

ReadInt16(IntPtr)

Lê um inteiro com sinal de 16 bits da memória não gerenciada.

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

Parâmetros

ptr
IntPtr

nativeint

O endereço na memória não gerenciada da qual ler.

Retornos

O inteiro com sinal de 16 bits lido da memória não gerenciada.

Atributos

Exceções

ptr não é um formato reconhecido.

-ou-

ptr é null.

-ou-

ptr é inválido.

Exemplos

O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando os métodos e WriteInt16 os ReadInt16 métodos.

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

O exemplo a seguir demonstra como usar o ReadInt16 método para ler o valor de uma variável não gerenciada 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);
}

Comentários

ReadInt16 tem um deslocamento implícito de 0. Esse método permite a interação direta com uma matriz de estilo Int16 C não gerenciada, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.

Há suporte para leitura de locais de memória não assinados.

Confira também

Aplica-se a

ReadInt16(IntPtr, Int32)

Lê um inteiro com sinal de 16 bits em um determinado deslocamento da memória não gerenciada.

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

Parâmetros

ptr
IntPtr

nativeint

O endereço base na memória não gerenciada da qual ler.

ofs
Int32

Um deslocamento de bytes adicional, que é adicionado ao parâmetro antes da ptr leitura.

Retornos

O inteiro com sinal de 16 bits lido da memória não gerenciada no deslocamento fornecido.

Atributos

Exceções

O endereço base (ptr) mais o byte de deslocamento (ofs) produz um endereço nulo ou inválido.

Exemplos

O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando os métodos e WriteInt16 os ReadInt16 métodos.

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

O exemplo a seguir demonstra como usar o ReadInt16 método para ler o valor de uma variável não gerenciada 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);
}

Comentários

ReadInt16 permite a interação direta com uma matriz assinada de 16 bits não gerenciada, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.

Há suporte para leitura de locais de memória não assinados.

Confira também

Aplica-se a

ReadInt16(Object, Int32)

Cuidado

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

Lê um inteiro com sinal de 16 bits em um determinado deslocamento da memória não gerenciada.

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);
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.")]
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
static member ReadInt16 : obj * int -> int16
[<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
Public Shared Function ReadInt16 (ptr As Object, ofs As Integer) As Short

Parâmetros

ptr
Object

O endereço base na memória não gerenciada do objeto de origem.

ofs
Int32

Um deslocamento de bytes adicional, que é adicionado ao parâmetro antes da ptr leitura.

Retornos

O inteiro com sinal de 16 bits lido da memória não gerenciada no deslocamento fornecido.

Atributos

Exceções

O endereço base (ptr) mais o byte de deslocamento (ofs) produz um endereço nulo ou inválido.

ptr é um ArrayWithOffset objeto. Esse método não aceita ArrayWithOffset parâmetros.

Comentários

ReadInt16 permite a interação direta com uma matriz assinada de 16 bits não gerenciada, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.

Há suporte para leitura de locais de memória não assinados.

Confira também

Aplica-se a