語言

Marshal.WriteByte 方法

定義

將一個位元組的值寫入未管理記憶體。

多載

名稱 Description
WriteByte(Object, Int32, Byte)
已淘汰.

在指定的偏移量處寫入一個位元組的記憶體。

WriteByte(IntPtr, Byte)

將一個位元組的值寫入未管理記憶體。

WriteByte(IntPtr, Int32, Byte)

在指定的偏移量處寫入一個位元組的記憶體。

WriteByte(Object, Int32, Byte)

來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
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.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
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.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
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

目標物件在未管理記憶體中的基底位址。

ofs
Int32

一個額外的位元組偏移量,會在寫入參數前加入 ptr 。

val
Byte

要寫入的值。

屬性

例外狀況

基底位址(ptr)加上偏移位元組(ofs)會產生空位址或無效位址。

ptr 是一個 ArrayWithOffset 物件。 此方法不接受 ArrayWithOffset 參數。

備註

WriteByte 它能直接與非管理的 C 風格位元組陣列互動,省去在設定元素值前先複製整個非管理陣列(使用 Marshal.Copy)到另一個管理陣列的成本。

另請參閱

適用於

WriteByte(IntPtr, Byte)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將一個位元組的值寫入未管理記憶體。

public:
 static void WriteByte(IntPtr ptr, System::Byte val);
public static void WriteByte(IntPtr ptr, byte val);
static member WriteByte : nativeint * byte -> unit
Public Shared Sub WriteByte (ptr As IntPtr, val As Byte)

參數

ptr
IntPtr

nativeint

寫入的非管理記憶體位址。

val
Byte

要寫入的值。

例外狀況

ptr 並非被認可的格式。

-或-

ptr 是 null。

-或-

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

備註

WriteByte 它能直接與非管理的 C 風格位元組陣列互動,省去在設定元素值前先複製整個非管理陣列(使用 Marshal.Copy)到另一個管理陣列的成本。

另請參閱

適用於

WriteByte(IntPtr, Int32, Byte)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

在指定的偏移量處寫入一個位元組的記憶體。

public:
 static void WriteByte(IntPtr ptr, int ofs, System::Byte val);
public static void WriteByte(IntPtr ptr, int ofs, byte val);
static member WriteByte : nativeint * int * byte -> unit
Public Shared Sub WriteByte (ptr As IntPtr, ofs As Integer, val As Byte)

參數

ptr
IntPtr

nativeint

寫入的非管理記憶體中的基底位址。

ofs
Int32

一個額外的位元組偏移量,會在寫入參數前加入 ptr 。

val
Byte

要寫入的值。

例外狀況

基底位址(ptr)加上偏移位元組(ofs)會產生空位址或無效位址。

範例

以下範例示範如何利用 ReadByte and 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)到另一個管理陣列的成本。

另請參閱

適用於