Upraviť

Zdieľať cez


Marshal.ReadIntPtr Method

Definition

Reads a processor native sized integer from unmanaged memory. Reading from unaligned memory locations is supported.

Overloads

ReadIntPtr(IntPtr, Int32)

Reads a processor native sized integer at a given offset from unmanaged memory.

ReadIntPtr(Object, Int32)
Obsolete.

Reads a processor native sized integer from unmanaged memory.

ReadIntPtr(IntPtr)

Reads a processor native-sized integer from unmanaged memory.

ReadIntPtr(IntPtr, Int32)

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

Reads a processor native sized integer at a given offset from unmanaged memory.

public:
 static IntPtr ReadIntPtr(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
public static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
[<System.Security.SecurityCritical>]
static member ReadIntPtr : nativeint * int -> nativeint
static member ReadIntPtr : nativeint * int -> nativeint
Public Shared Function ReadIntPtr (ptr As IntPtr, ofs As Integer) As IntPtr

Parameters

ptr
IntPtr

nativeint

The base address in unmanaged memory from which to read.

ofs
Int32

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

Returns

IntPtr

nativeint

The integer read from unmanaged memory at the given offset.

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

ReadIntPtr 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 reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

ReadIntPtr(Object, Int32)

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

Caution

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

Reads a processor native sized integer from unmanaged memory.

public:
 static IntPtr ReadIntPtr(System::Object ^ ptr, int ofs);
[System.Obsolete("ReadIntPtr(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static IntPtr ReadIntPtr (object ptr, int ofs);
[System.Obsolete("ReadIntPtr(Object, Int32) may be unavailable in future releases.")]
public static IntPtr ReadIntPtr (object ptr, int ofs);
public static IntPtr ReadIntPtr (object ptr, int ofs);
[System.Security.SecurityCritical]
public static IntPtr ReadIntPtr (object ptr, int ofs);
[<System.Obsolete("ReadIntPtr(Object, Int32) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member ReadIntPtr : obj * int -> nativeint
[<System.Obsolete("ReadIntPtr(Object, Int32) may be unavailable in future releases.")>]
static member ReadIntPtr : obj * int -> nativeint
static member ReadIntPtr : obj * int -> nativeint
[<System.Security.SecurityCritical>]
static member ReadIntPtr : obj * int -> nativeint
Public Shared Function ReadIntPtr (ptr As Object, ofs As Integer) As IntPtr

Parameters

ptr
Object

The base address in unmanaged memory of the source object.

ofs
Int32

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

Returns

IntPtr

nativeint

The integer read from unmanaged memory at the given offset.

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

ReadIntPtr 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 reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

ReadIntPtr(IntPtr)

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

Reads a processor native-sized integer from unmanaged memory.

public:
 static IntPtr ReadIntPtr(IntPtr ptr);
[System.Security.SecurityCritical]
public static IntPtr ReadIntPtr (IntPtr ptr);
public static IntPtr ReadIntPtr (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadIntPtr : nativeint -> nativeint
static member ReadIntPtr : nativeint -> nativeint
Public Shared Function ReadIntPtr (ptr As IntPtr) As IntPtr

Parameters

ptr
IntPtr

nativeint

The address in unmanaged memory from which to read.

Returns

IntPtr

nativeint

The integer read from unmanaged memory. A 32 bit integer is returned on 32 bit machines and a 64 bit integer is returned on 64 bit machines.

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

ReadIntPtr has an implied offset of 0. This method 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 reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to