Marshal.WriteInt32 Marshal.WriteInt32 Marshal.WriteInt32 Marshal.WriteInt32 Method

Definition

Writes a 32-bit signed integer value to unmanaged memory. Writing to unaligned memory locations is supported.

Overloads

WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32)

Writes a 32-bit signed integer value to unmanaged memory.

WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32)

Writes a 32-bit signed integer value into unmanaged memory at a specified offset.

WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32)

Writes a 32-bit signed integer value to unmanaged memory at a specified offset.

WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32) WriteInt32(IntPtr, Int32)

Writes a 32-bit signed integer value to unmanaged memory.

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

The address in unmanaged memory to write to.

val
Int32 Int32 Int32 Int32

The value to write.

Exceptions

ptr is not a recognized format.

-or-

ptr is null.

-or-

ptr is invalid.

Examples

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

static void ReadWriteInt32()
{
    // Allocate unmanaged memory. 
    int elementSize = 4;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(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.ReadInt32(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt32()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 4
    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.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
    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.ReadInt32(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

Remarks

WriteInt32 enables direct interaction with an unmanaged 32-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

Security

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

See Also

WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32) WriteInt32(IntPtr, Int32, Int32)

Writes a 32-bit signed integer value into unmanaged memory at a specified offset.

public:
 static void WriteInt32(IntPtr ptr, int ofs, int val);
[System.Security.SecurityCritical]
public static void WriteInt32 (IntPtr ptr, int ofs, int val);
static member WriteInt32 : nativeint * int * int -> unit
Public Shared Sub WriteInt32 (ptr As IntPtr, ofs As Integer, val As Integer)
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
Int32 Int32 Int32 Int32

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 ReadInt32 and WriteInt32 methods.

static void ReadWriteInt32()
{
    // Allocate unmanaged memory. 
    int elementSize = 4;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(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.ReadInt32(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt32()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 4
    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.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
    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.ReadInt32(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

Remarks

WriteInt32 enables direct interaction with an unmanaged 32-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

Security

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

See Also

WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32) WriteInt32(Object, Int32, Int32)

Warning

This API is now obsolete.

Writes a 32-bit signed integer value to unmanaged memory at a specified offset.

public:
 static void WriteInt32(System::Object ^ ptr, int ofs, int val);
[System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt32 (object ptr, int ofs, int val);
static member WriteInt32 : obj * int * int -> unit
Public Shared Sub WriteInt32 (ptr As Object, ofs As Integer, val As Integer)
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
Int32 Int32 Int32 Int32

The value to write.

Exceptions

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

Remarks

WriteInt32 enables direct interaction with an unmanaged 32-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before setting its element values.

Writing to unaligned memory locations is supported.

Security

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

See Also

Applies to