Marshal.ReadIntPtr 方法

定义

从非托管内存读取处理器本机大小的整数。 支持从未对齐的内存位置进行读取。

重载

ReadIntPtr(IntPtr, Int32)

从非托管内存按给定的偏移量读取处理器本机大小的整数。

ReadIntPtr(Object, Int32)
已过时.

从非托管内存读取处理器本机大小的整数。

ReadIntPtr(IntPtr)

从非托管内存读取处理器本机大小的整数。

ReadIntPtr(IntPtr, Int32)

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

从非托管内存按给定的偏移量读取处理器本机大小的整数。

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

参数

ptr
IntPtr

nativeint

非托管内存中开始读取的基址。

ofs
Int32

额外的字节偏移量,在读取前添加到 ptr 参数中。

返回

IntPtr

nativeint

从非托管内存按给定的偏移量读取的整数。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。

示例

以下示例演示如何使用 ReadIntPtrWriteIntPtr 方法读取和写入非托管数组。

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

注解

ReadIntPtr 支持与非托管 C 样式 IntPtr 数组的直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。

支持从未对齐的内存位置进行读取。

另请参阅

适用于

ReadIntPtr(Object, Int32)

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

注意

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

从非托管内存读取处理器本机大小的整数。

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

参数

ptr
Object

非托管内存中源对象的基址。

ofs
Int32

额外的字节偏移量,在读取前添加到 ptr 参数中。

返回

IntPtr

nativeint

从非托管内存按给定的偏移量读取的整数。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。

ptrArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

ReadIntPtr 支持与非托管 C 样式 IntPtr 数组的直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。

支持从未对齐的内存位置进行读取。

另请参阅

适用于

ReadIntPtr(IntPtr)

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

从非托管内存读取处理器本机大小的整数。

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

参数

ptr
IntPtr

nativeint

非托管内存中开始读取的地址。

返回

IntPtr

nativeint

从非托管内存读取的整数。 在 32 位计算机上返回 32 位整数,在 64 位计算机上返回 64 位整数。

属性

例外

ptr 不是识别的格式。

- 或 -

ptrnull

- 或 -

ptr 无效。

示例

以下示例演示如何使用 ReadIntPtrWriteIntPtr 方法读取和写入非托管数组。

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

注解

ReadIntPtr 具有 0 的隐式偏移量。 此方法允许与非托管 C 样式 IntPtr 数组直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。

支持从未对齐的内存位置进行读取。

另请参阅

适用于