Marshal.WriteIntPtr Method

Definition

Writes a processor native-sized integer value to unmanaged memory. 32-bit integers are written on 32-bit systems, and 64-bit integers are written on 64-bit systems. Writing to unaligned memory locations is supported.

Overloads

WriteIntPtr(IntPtr, IntPtr)

Writes a processor native sized integer value into unmanaged memory.

WriteIntPtr(IntPtr, Int32, IntPtr)

Writes a processor native-sized integer value to unmanaged memory at a specified offset.

WriteIntPtr(Object, Int32, IntPtr)
Obsolete.

Writes a processor native sized integer value to unmanaged memory.

WriteIntPtr(IntPtr, IntPtr)

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

Writes a processor native sized integer value into unmanaged memory.

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

Parameters

ptr
IntPtr

The address in unmanaged memory to write to.

val
IntPtr

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 ReadIntPtr and WriteIntPtr methods.

C#
static void ReadWriteIntPtr()
{
    // Allocate unmanaged memory. 
    int elementSize = Marshal.SizeOf(typeof(IntPtr));
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

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

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}

Remarks

WriteIntPtr enables direct interaction with an unmanaged C-style IntPtr 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 9 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
.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

WriteIntPtr(IntPtr, Int32, IntPtr)

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

Writes a processor native-sized integer value to unmanaged memory at a specified offset.

C#
[System.Security.SecurityCritical]
public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val);
C#
public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr 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
IntPtr

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 ReadIntPtr and WriteIntPtr methods.

C#
static void ReadWriteIntPtr()
{
    // Allocate unmanaged memory. 
    int elementSize = Marshal.SizeOf(typeof(IntPtr));
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

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

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}

Remarks

This method writes a 32 bit integer on 32 bit systems, and a 64 bit integer on 64 bit systems.

WriteIntPtr enables direct interaction with an unmanaged C-style IntPtr 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 9 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
.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

WriteIntPtr(Object, Int32, IntPtr)

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

Caution

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

Writes a processor native sized integer value to unmanaged memory.

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

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

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

Writing to unaligned memory locations is supported.

See also

Applies to

.NET 9 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)
.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 (4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided))
.NET Standard 1.1, 2.0 (1.2, 1.3, 1.4, 1.5, 1.6, 2.1)
UWP (10.0)