Marshal.WriteByte 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将单个字节值写入到非托管内存。
重载
| WriteByte(Object, Int32, Byte) | 
		已过时.
	 按指定偏移量将单字节值写入非托管内存。 | 
| WriteByte(IntPtr, Byte) | 将单个字节值写入到非托管内存。 | 
| WriteByte(IntPtr, Int32, Byte) | 按指定偏移量将单字节值写入非托管内存。 | 
WriteByte(Object, Int32, Byte)
- Source:
- Marshal.CoreCLR.cs
- Source:
- Marshal.CoreCLR.cs
- Source:
- Marshal.CoreCLR.cs
注意
WriteByte(Object, Int32, Byte) may be unavailable in future releases.
按指定偏移量将单字节值写入非托管内存。
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 -> unitstatic member WriteByte : obj * int * byte -> unit[<System.Security.SecurityCritical>]
static member WriteByte : obj * int * byte -> unitPublic Shared Sub WriteByte (ptr As Object, ofs As Integer, val As Byte)参数
- ptr
- Object
非托管内存中目标对象的基址。
- ofs
- Int32
额外的字节偏移量,在写入前添加到 ptr 参数中。
- val
- Byte
要写入的值。
- 属性
例外
基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。
              ptr 是 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。
注解
WriteByte 允许与非托管 C 样式字节数组直接交互,无需在设置其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组。
另请参阅
适用于
WriteByte(IntPtr, Byte)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
将单个字节值写入到非托管内存。
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 -> unitstatic member WriteByte : nativeint * byte -> unitPublic Shared Sub WriteByte (ptr As IntPtr, val As Byte)参数
- ptr
- 
				
				IntPtr
nativeint 
非托管内存中要写入的地址。
- val
- Byte
要写入的值。
- 属性
例外
示例
以下示例创建非托管内存块,将字节写入非托管内存,从非托管内存中读取字节,然后释放非托管内存。
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 允许与非托管 C 样式字节数组直接交互,无需在设置其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组。
另请参阅
适用于
WriteByte(IntPtr, Int32, Byte)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
按指定偏移量将单字节值写入非托管内存。
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 -> unitstatic member WriteByte : nativeint * int * byte -> unitPublic Shared Sub WriteByte (ptr As IntPtr, ofs As Integer, val As Byte)参数
- ptr
- 
				
				IntPtr
nativeint 
非托管内存中要写入的基址。
- ofs
- Int32
额外的字节偏移量,在写入前添加到 ptr 参数中。
- val
- Byte
要写入的值。
- 属性
例外
基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。
示例
以下示例演示如何使用 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
注解
WriteByte 允许与非托管 C 样式字节数组直接交互,无需在设置其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组。