Marshal.WriteByte 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將單一位元組值寫入 Unmanaged 記憶體。
多載
WriteByte(Object, Int32, Byte) |
已淘汰.
將單一位元組值寫入 Unmanaged 記憶體中的指定位移。 |
WriteByte(IntPtr, Byte) |
將單一位元組值寫入 Unmanaged 記憶體。 |
WriteByte(IntPtr, Int32, Byte) |
將單一位元組值寫入 Unmanaged 記憶體中的指定位移。 |
WriteByte(Object, Int32, Byte)
警告
WriteByte(Object, Int32, Byte) may be unavailable in future releases.
將單一位元組值寫入 Unmanaged 記憶體中的指定位移。
public:
static void WriteByte(System::Object ^ ptr, int ofs, System::Byte val);
[System.Obsolete("WriteByte(Object, Int32, Byte) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteByte (object ptr, int ofs, byte val);
[System.Obsolete("WriteByte(Object, Int32, Byte) may be unavailable in future releases.")]
public static void WriteByte (object ptr, int ofs, byte val);
public static void WriteByte (object ptr, int ofs, byte val);
[System.Security.SecurityCritical]
public static void WriteByte (object ptr, int ofs, byte val);
[<System.Obsolete("WriteByte(Object, Int32, Byte) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member WriteByte : obj * int * byte -> unit
[<System.Obsolete("WriteByte(Object, Int32, Byte) may be unavailable in future releases.")>]
static member WriteByte : obj * int * byte -> unit
static member WriteByte : obj * int * byte -> unit
[<System.Security.SecurityCritical>]
static member WriteByte : obj * int * byte -> unit
Public Shared Sub WriteByte (ptr As Object, ofs As Integer, val As Byte)
參數
- ptr
- Object
目標物件之 Unmanaged 記憶體中的基底位址。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行寫入。
- val
- Byte
要寫入的值。
- 屬性
例外狀況
基底位址 (ptr
) 加上位移位元組 (ofs
) 會產生 Null 或無效的位址。
ptr
是 ArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。
備註
WriteByte 可讓您直接與 Unmanaged C 樣式的位元組陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣列複製到個別 Managed 陣列,再設定其元素值之前,先將整個 Unmanaged 陣列複製到 (。
另請參閱
適用於
WriteByte(IntPtr, Byte)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
將單一位元組值寫入 Unmanaged 記憶體。
public:
static void WriteByte(IntPtr ptr, System::Byte val);
[System.Security.SecurityCritical]
public static void WriteByte (IntPtr ptr, byte val);
public static void WriteByte (IntPtr ptr, byte val);
[<System.Security.SecurityCritical>]
static member WriteByte : nativeint * byte -> unit
static member WriteByte : nativeint * byte -> unit
Public Shared Sub WriteByte (ptr As IntPtr, val As Byte)
參數
- ptr
-
IntPtr
nativeint
Unmanaged 記憶體中要寫入的位址。
- val
- Byte
要寫入的值。
- 屬性
例外狀況
範例
下列範例會建立 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
備註
WriteByte 可讓您直接與 Unmanaged C 樣式的位元組陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣列複製到個別 Managed 陣列,再設定其元素值之前,先將整個 Unmanaged 陣列複製到 (。
另請參閱
適用於
WriteByte(IntPtr, Int32, Byte)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
將單一位元組值寫入 Unmanaged 記憶體中的指定位移。
public:
static void WriteByte(IntPtr ptr, int ofs, System::Byte val);
[System.Security.SecurityCritical]
public static void WriteByte (IntPtr ptr, int ofs, byte val);
public static void WriteByte (IntPtr ptr, int ofs, byte val);
[<System.Security.SecurityCritical>]
static member WriteByte : nativeint * int * byte -> unit
static member WriteByte : nativeint * int * byte -> unit
Public Shared Sub WriteByte (ptr As IntPtr, ofs As Integer, val As Byte)
參數
- ptr
-
IntPtr
nativeint
Unmanaged 記憶體中要寫入的基底位址。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行寫入。
- val
- Byte
要寫入的值。
- 屬性
例外狀況
基底位址 (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
備註
WriteByte 可讓您直接與 Unmanaged C 樣式的位元組陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣列複製到個別 Managed 陣列,再設定其元素值之前,先將整個 Unmanaged 陣列複製到 (。