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.

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

Parameters

ptr
IntPtr

nativeint

The address in unmanaged memory to write to.

val
IntPtr

nativeint

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.

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();
}
Sub ReadWriteIntPtr()
    ' Allocate unmanaged memory.
    Dim elementSize As Integer = Marshal.SizeOf(GetType(IntPtr))
    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.WriteIntPtr(unmanagedArray, i * elementSize, CType(i + 1, IntPtr))
    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.ReadIntPtr(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

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

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

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.

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

Parameters

ptr
IntPtr

nativeint

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

nativeint

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.

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();
}
Sub ReadWriteIntPtr()
    ' Allocate unmanaged memory.
    Dim elementSize As Integer = Marshal.SizeOf(GetType(IntPtr))
    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.WriteIntPtr(unmanagedArray, i * elementSize, CType(i + 1, IntPtr))
    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.ReadIntPtr(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

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

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

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.

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

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

nativeint

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