Marshal.WriteByte Marshal.WriteByte Marshal.WriteByte Marshal.WriteByte Method

Definition

Writes a single byte value to unmanaged memory.

Overloads

WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte)

Writes a single byte value to unmanaged memory at a specified offset.

WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte)

Writes a single byte value to unmanaged memory at a specified offset.

WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte)

Writes a single byte value to unmanaged memory.

WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte) WriteByte(Object, Int32, Byte)

Warning

This API is now obsolete.

Writes a single byte value to unmanaged memory at a specified offset.

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);
static member WriteByte : obj * int * byte -> unit
Public Shared Sub WriteByte (ptr As Object, ofs As Integer, val As Byte)
Parameters
ptr
Object Object Object Object

The base address in unmanaged memory of the target object.

ofs
Int32 Int32 Int32 Int32

An additional byte offset, which is added to the ptr parameter before writing.

val
Byte Byte Byte Byte

The value to write.

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Remarks

WriteByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

See Also

WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte) WriteByte(IntPtr, Int32, Byte)

Writes a single byte value to unmanaged memory at a specified offset.

public:
 static void WriteByte(IntPtr ptr, int ofs, System::Byte val);
[System.Security.SecurityCritical]
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)
Parameters
ptr
IntPtr IntPtr IntPtr IntPtr

The base address in unmanaged memory to write to.

ofs
Int32 Int32 Int32 Int32

An additional byte offset, which is added to the ptr parameter before writing.

val
Byte Byte Byte Byte

The value to write.

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadByte and WriteByte methods.

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

Remarks

WriteByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

See Also

WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte) WriteByte(IntPtr, Byte)

Writes a single byte value to unmanaged memory.

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

The address in unmanaged memory to write to.

val
Byte Byte Byte Byte

The value to write.

Exceptions

ptr is not a recognized format.

-or-

ptr is null.

-or-

ptr is invalid.

Examples

The following example creates a block of unmanaged memory, writes a byte to the unmanaged memory, reads the byte back from unmanaged memory, and then disposes the unmanaged memory.

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
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

Remarks

WriteByte enables direct interaction with an unmanaged C-style byte array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

See Also

Applies to