Marshal.ReadByte 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 Unmanaged 記憶體讀取單一位元組。 支援讀取未配置的記憶體位置。
多載
ReadByte(IntPtr, Int32) |
從 Unmanaged 記憶體讀取在指定位移 (或索引) 的單一位元組。 |
ReadByte(Object, Int32) |
已淘汰.
從 Unmanaged 記憶體讀取在指定位移 (或索引) 的單一位元組。 |
ReadByte(IntPtr) |
從 Unmanaged 記憶體讀取單一位元組。 |
ReadByte(IntPtr, Int32)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
從 Unmanaged 記憶體讀取在指定位移 (或索引) 的單一位元組。
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
從 Unmanaged 記憶體中讀取的基底位址 (Base Address)。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行讀取。
傳回
從 Unmanaged 記憶體中指定位移處讀取的位元組。
- 屬性
例外狀況
基底位址 (ptr
) 加上位移位元組 (ofs
) 會產生 Null 或無效的位址。
範例
下列範例示範如何使用 和 WriteByte 方法來讀取和寫入 Unmanaged 陣列ReadByte。
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 方法來讀取 Unmanaged 字元的值。
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 可讓您與 Unmanaged C 樣式位元組陣列直接互動,而不需要先使用 Marshal.Copy) 將整個 Unmanaged 數位複製到個別 Managed 陣列,再讀取其元素值,即可將整個 Unmanaged 陣列複製 (。
支援讀取未配置的記憶體位置。
另請參閱
適用於
ReadByte(Object, Int32)
警告
ReadByte(Object, Int32) may be unavailable in future releases.
從 Unmanaged 記憶體讀取在指定位移 (或索引) 的單一位元組。
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
來源物件之 Unmanaged 記憶體中的基底位址。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行讀取。
傳回
從 Unmanaged 記憶體中指定位移處讀取的位元組。
- 屬性
例外狀況
基底位址 (ptr
) 加上位移位元組 (ofs
) 會產生 Null 或無效的位址。
ptr
是 ArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。
備註
ReadByte 可讓您與 Unmanaged C 樣式位元組陣列直接互動,而不需要先使用 Marshal.Copy) 將整個 Unmanaged 數位複製到個別 Managed 陣列,再讀取其元素值,即可將整個 Unmanaged 陣列複製 (。
支援讀取未配置的記憶體位置。
另請參閱
適用於
ReadByte(IntPtr)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
從 Unmanaged 記憶體讀取單一位元組。
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
從 Unmanaged 記憶體中讀取的位址。
傳回
從 Unmanaged 記憶體讀取的位元組。
- 屬性
例外狀況
範例
下列範例會建立 Unmanaged 記憶體區塊、將位元組寫入 Unmanaged 記憶體、從 Unmanaged 記憶體讀取位元元組,然後處置 Unmanaged 記憶體。
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 方法來讀取 Unmanaged 字元的值。
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 的隱含位移。 這個方法可讓您與 Unmanaged C 樣式位元組陣列直接互動,而不需要在讀取其元素值之前,先) 將 Marshal.Copy 整個 Unmanaged 陣組複製 ( (。
支援讀取未配置的記憶體位置。