Método Marshal.ReadByte (IntPtr, Int32)
Dica
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.
Lê um único byte em um determinado deslocamento (ou índice) de memória não gerenciada.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SecurityCriticalAttribute]
public static byte ReadByte(
IntPtr ptr,
int ofs
)
public:
[SecurityCriticalAttribute]
static unsigned char ReadByte(
IntPtr ptr,
int ofs
)
[<SecurityCriticalAttribute>]
static member ReadByte :
ptr:nativeint *
ofs:int -> byte
<SecurityCriticalAttribute>
Public Shared Function ReadByte (
ptr As IntPtr,
ofs As Integer
) As Byte
Parâmetros
ptr
Type: System.IntPtrO endereço básico na memória não gerenciada no qual a leitura será realizada.
ofs
Type: System.Int32Um deslocamento de byte adicional, adicionado ao parâmetro ptr antes de ler.
Valor Retornado
Type: System.Byte
O byte lido da memória não gerenciada em um determinado deslocamento.
Exceções
Exception | Condition |
---|---|
AccessViolationException | O endereço básico (ptr) e o deslocamento de byte (ofs) produz um endereço nulo ou inválido. |
Comentários
ReadByteHabilita interação direta com uma matriz de bytes de estilo C não gerenciada, eliminando a despesa de cópia de uma matriz não gerenciada toda (usando Marshal.Copy) em uma matriz separada gerenciada antes de ler os valores de elemento.
Há suporte para leitura de locais de memória desalinhados.
Exemplos
O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando o ReadByte e WriteByte métodos.
static void ReadWriteByte()
{
// Allocate unmanaged memory.
int elementSize = 1;
IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);
// Set the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
{
Marshal.WriteByte(unmanagedArray, i * elementSize, ((Byte)(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.ReadByte(unmanagedArray, i * elementSize));
}
Marshal.FreeHGlobal(unmanagedArray);
Console.WriteLine("Done. Press Enter to continue.");
Console.ReadLine();
}
Sub ReadWriteByte()
' Allocate unmanaged memory.
Dim elementSize As Integer = 1
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.WriteByte(unmanagedArray, i * elementSize, CType(i + 1, Byte))
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.ReadByte(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 ReadByte método para ler o valor de um caractere não gerenciado.
using namespace System;
using namespace System::Runtime::InteropServices;
void main()
{
// Create an unmanaged byte.
const char * myString = "bB";
// Read the second character of the c string as a managed byte.
Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString, 1);
// Display the byte to the console.
Console::WriteLine(myManagedByte);
}
Segurança
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Silverlight
Disponível desde 2.0
Windows Phone Silverlight
Disponível desde 7.0
Windows Phone
Disponível desde 8.1
Confira Também
Copy
ReadByte Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices
Retornar ao início