Marshal.ReadByte メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
アンマネージ メモリから 1 バイトを読み取ります。 アライメントされていないメモリ位置からの読み取りがサポートされています。
ReadByte(IntPtr, Int32) |
アンマネージ メモリから、指定されたオフセット (またはインデックス) で 1 バイトを読み取ります。 |
ReadByte(Object, Int32) |
古い.
アンマネージ メモリから、指定されたオフセット (またはインデックス) で 1 バイトを読み取ります。 |
ReadByte(IntPtr) |
アンマネージ メモリから 1 バイトを読み取ります。 |
- ソース:
- Marshal.cs
- ソース:
- Marshal.cs
- ソース:
- Marshal.cs
アンマネージ メモリから、指定されたオフセット (またはインデックス) で 1 バイトを読み取ります。
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
) は、null または無効なアドレスを生成します。
例
次の例では、ReadByte メソッドと 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を使用して) アンマネージド配列全体を別のマネージド配列にコピーする手間が省けます。
アライメントされていないメモリ位置からの読み取りがサポートされています。
こちらもご覧ください
適用対象
.NET 9 およびその他のバージョン
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
注意事項
ReadByte(Object, Int32) may be unavailable in future releases.
アンマネージ メモリから、指定されたオフセット (またはインデックス) で 1 バイトを読み取ります。
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
) は、null または無効なアドレスを生成します。
ptr
は ArrayWithOffset オブジェクトです。 このメソッドは、ArrayWithOffset パラメーターを受け取りません。
注釈
ReadByte を使用すると、アンマネージ C スタイルのバイト配列と直接やり取りできるため、要素値を読み取る前に (Marshal.Copyを使用して) アンマネージド配列全体を別のマネージド配列にコピーする手間が省けます。
アライメントされていないメモリ位置からの読み取りがサポートされています。
こちらもご覧ください
適用対象
.NET 9 およびその他のバージョン
製品 | バージョン (廃止) |
---|---|
.NET | (Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9) |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1) |
UWP | (10.0) |
- ソース:
- Marshal.cs
- ソース:
- Marshal.cs
- ソース:
- Marshal.cs
アンマネージ メモリから 1 バイトを読み取ります。
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
読み取り元のアンマネージ メモリ内のアドレス。
戻り値
アンマネージ メモリから読み取られたバイト。
- 属性
例外
例
次の例では、アンマネージ メモリのブロックを作成し、アンマネージ メモリにバイトを書き込み、アンマネージ メモリからバイトを読み取り、アンマネージ メモリを破棄します。
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を使用) を別のマネージド配列にコピーする手間が省けます。
アライメントされていないメモリ位置からの読み取りがサポートされています。
こちらもご覧ください
適用対象
.NET 9 およびその他のバージョン
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。