Marshal.ReadByte 方法

定義

從未管理的記憶體讀取一個位元組。 支援從未對齊的記憶體位置讀取。

多載

名稱 Description
ReadByte(IntPtr, Int32)

從未管理記憶體讀取指定偏移量(或索引)的一個位元組。

ReadByte(Object, Int32)
已淘汰.

從未管理記憶體讀取指定偏移量(或索引)的一個位元組。

ReadByte(IntPtr)

從未管理的記憶體讀取一個位元組。

ReadByte(IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從未管理記憶體讀取指定偏移量(或索引)的一個位元組。

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

參數

ptr
IntPtr

nativeint

未管理記憶體中的基底位址,供讀取。

ofs
Int32

額外的位元組偏移量,會在讀取參數前加 ptr 到參數上。

傳回

位元組從未管理記憶體在指定偏移量讀取。

屬性

例外狀況

基底位址(ptr)加上偏移位元組(ofs)會產生空位址或無效位址。

範例

以下範例示範如何利用 ReadByte and 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

以下範例示範如何使用此 ReadByte 方法讀取未管理字元的值。


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

備註

ReadByte 它允許直接與非管理的 C 風格位元組陣列互動,免除了在讀取元素值前將整個非管理陣列 Marshal.Copy複製到獨立管理陣列的成本。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於

ReadByte(Object, Int32)

來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs

警告

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

從未管理記憶體讀取指定偏移量(或索引)的一個位元組。

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.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
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.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
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

參數

ptr
Object

原始物件在非管理記憶體中的基底位址。

ofs
Int32

額外的位元組偏移量,會在讀取參數前加 ptr 到參數上。

傳回

位元組從未管理記憶體在指定偏移量讀取。

屬性

例外狀況

基底位址(ptr)加上偏移位元組(ofs)會產生空位址或無效位址。

ptr 是一個 ArrayWithOffset 物件。 此方法不接受 ArrayWithOffset 參數。

備註

ReadByte 它允許直接與非管理的 C 風格位元組陣列互動,免除了在讀取元素值前將整個非管理陣列 Marshal.Copy複製到獨立管理陣列的成本。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於

ReadByte(IntPtr)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

從未管理的記憶體讀取一個位元組。

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

參數

ptr
IntPtr

nativeint

未管理記憶體中的位址,用於讀取。

傳回

位元組是從未管理的記憶體讀取的。

屬性

例外狀況

ptr 並非被認可的格式。

-或-

ptrnull

-或-

ptr 無效。

範例

以下範例建立一個非管理記憶體區塊,寫入一個位元組到該記憶體,再從非管理記憶體讀取該位元組,然後丟棄該非管理記憶體。

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

以下範例示範如何使用此 ReadByte 方法讀取未管理字元的值。


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

備註

ReadByte 隱含偏移量為0。 此方法允許直接與非管理的 C 風格位元組陣列互動,省去在讀取元素值前將整個非管理陣列 Marshal.Copy複製到獨立管理陣列的成本。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於