Marshal.WriteInt32 Method

Definition

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

Overloads

WriteInt32(Object, Int32, Int32)
Obsolete.

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

WriteInt32(IntPtr, Int32)

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

WriteInt32(IntPtr, Int32, Int32)

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

WriteInt32(Object, Int32, Int32)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

Caution

WriteInt32(Object, Int32, Int32) may be unavailable in future releases.

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

C#
[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);
C#
[System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")]
public static void WriteInt32(object ptr, int ofs, int val);
C#
public static void WriteInt32(object ptr, int ofs, int val);
C#
[System.Security.SecurityCritical]
public static void WriteInt32(object ptr, int ofs, int val);

Parameters

ptr
Object

The base address in unmanaged memory of the target object.

ofs
Int32

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

val
Int32

The value to write.

Attributes

Exceptions

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

ptr is an ArrayWithOffset object. This method does not accept ArrayWithOffset parameters.

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.

See also

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.NET (Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1)
UWP (10.0)

WriteInt32(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

C#
[System.Security.SecurityCritical]
public static void WriteInt32(IntPtr ptr, int val);
C#
public static void WriteInt32(IntPtr ptr, int val);

Parameters

ptr
IntPtr

The address in unmanaged memory to write to.

val
Int32

The value to write.

Attributes

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.

C#
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();
}

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

WriteInt32(IntPtr, Int32, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

C#
[System.Security.SecurityCritical]
public static void WriteInt32(IntPtr ptr, int ofs, int val);
C#
public static void WriteInt32(IntPtr ptr, int ofs, int val);

Parameters

ptr
IntPtr

The base address in unmanaged memory to write to.

ofs
Int32

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

val
Int32

The value to write.

Attributes

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.

C#
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();
}

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0