Marshal.ReadByte 方法

定义

从非托管内存读取单个字节。 支持从未对齐的内存位置进行读取。

重载

ReadByte(IntPtr, Int32)

从非托管内存按给定的偏移量(或索引)读取单个字节。

ReadByte(Object, Int32)
已过时.

从非托管内存按给定的偏移量(或索引)读取单个字节。

ReadByte(IntPtr)

从非托管内存读取单个字节。

ReadByte(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
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) 可产生空或无效地址。

示例

以下示例演示如何使用 ReadByteWriteByte 方法读取和写入非托管数组。

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)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
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.")]
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

参数

ptr
Object

非托管内存中源对象的基址。

ofs
Int32

额外的字节偏移量,在读取前添加到 ptr 参数中。

返回

从非托管内存按给定的偏移量读取的字节。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。

ptrArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

ReadByte 支持与非托管 C 样式字节数组的直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组的开销。

支持从未对齐的内存位置进行读取。

另请参阅

适用于

ReadByte(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
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) 将整个非托管数组 (复制到单独的托管数组的开销。

支持从未对齐的内存位置进行读取。

另请参阅

适用于