Marshal.ReadByte Metodo

Definizione

Legge un singolo byte da memoria non gestita. La lettura da posizioni di memoria non allineate è supportata.

Overload

ReadByte(IntPtr, Int32)

Legge un singolo byte in base a un determinato offset (o indice) dalla memoria non gestita.

ReadByte(Object, Int32)
Obsoleti.

Legge un singolo byte in base a un determinato offset (o indice) dalla memoria non gestita.

ReadByte(IntPtr)

Legge un singolo byte da memoria non gestita.

ReadByte(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Legge un singolo byte in base a un determinato offset (o indice) dalla memoria non gestita.

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

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

Byte letto da memoria non gestita all'offset fornito.

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 ReadByte metodi e WriteByte .

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

Nell'esempio seguente viene illustrato come utilizzare il ReadByte metodo per leggere il valore di un carattere non gestito.


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);
}

Commenti

ReadByte consente l'interazione diretta con una matrice di byte in stile C 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

ReadByte(Object, Int32)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

Attenzione

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

Legge un singolo byte in base a un determinato offset (o indice) dalla memoria non gestita.

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

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

Byte letto da memoria non gestita all'offset fornito.

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

ReadByte consente l'interazione diretta con una matrice di byte in stile C 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

ReadByte(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Legge un singolo byte da memoria non gestita.

public:
 static System::Byte ReadByte(IntPtr ptr);
[System.Security.SecurityCritical]
public static byte ReadByte (IntPtr ptr);
public static byte ReadByte (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadByte : nativeint -> byte
static member ReadByte : nativeint -> byte
Public Shared Function ReadByte (ptr As IntPtr) As Byte

Parametri

ptr
IntPtr

nativeint

Indirizzo nella memoria non gestita da cui leggere.

Restituisce

Byte letto da memoria non gestita.

Attributi

Eccezioni

ptr non è un formato riconosciuto.

-oppure-

ptr è null.

-oppure-

ptr non è valido.

Esempio

L'esempio seguente crea un blocco di memoria non gestita, scrive un byte nella memoria non gestita, legge il byte dalla memoria non gestita e quindi elimina la memoria non gestita.

using System;
using System.Runtime.InteropServices;

 class Example
 {
     static void Main(string[] args)
     {
          // Allocate 1 byte of unmanaged memory.
          IntPtr hGlobal = Marshal.AllocHGlobal(1);

          // Create a new byte.
          byte b = 1;
          Console.WriteLine("Byte written to unmanaged memory: " + b);

          // Write the byte to unmanaged memory.
          Marshal.WriteByte(hGlobal, b);

          // Read byte from unmanaged memory.
          byte c = Marshal.ReadByte(hGlobal);
          Console.WriteLine("Byte read from unmanaged memory: " + c);

          // Free the unmanaged memory.
          Marshal.FreeHGlobal(hGlobal);
          Console.WriteLine("Unmanaged memory was disposed.");
     }
}
Imports System.Runtime.InteropServices

Module Example
    Sub Main()
         ' Allocate 1 byte of unmanaged memory.
         Dim hGlobal As IntPtr = Marshal.AllocHGlobal(1)
         
         ' Create a new byte.
         Dim b As Byte = 1
         
         Console.WriteLine("Byte written to unmanaged memory: {0}", b)
         
         ' Write the byte to unmanaged memory.
         Marshal.WriteByte(hGlobal, b)
         
         ' Read byte from unmanaged memory.
         Dim c As Byte = Marshal.ReadByte(hGlobal)
         Console.WriteLine("Byte read from unmanaged memory: {0}", c)
         
         ' Free the unmanaged memory.
         Marshal.FreeHGlobal(hGlobal)
         Console.WriteLine("Unmanaged memory was disposed.")
    End Sub
End Module

Nell'esempio seguente viene illustrato come utilizzare il ReadByte metodo per leggere il valore di un carattere non gestito.


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



void main()
{
    // Create an unmanaged byte.
    const char * myString = "b";

    // Read the c string as a managed byte.
        Byte ^ myManagedByte = Marshal::ReadByte((IntPtr) (char *) myString);

    // Display the byte to the console.
    Console::WriteLine(myManagedByte);
}

Commenti

ReadByte ha un offset implicito pari a 0. Questo metodo consente l'interazione diretta con una matrice di byte in stile C non gestito, 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