Marshal.ReadIntPtr 方法

定义

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

重载

名称 说明
ReadIntPtr(IntPtr, Int32)

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

ReadIntPtr(Object, Int32)
已过时.

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

ReadIntPtr(IntPtr)

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

ReadIntPtr(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
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) 生成 null 或无效地址。

示例

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

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 数组的直接交互,从而消除了在读取其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

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

另请参阅

适用于

ReadIntPtr(Object, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
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.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
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.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
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) 生成 null 或无效地址。

ptr 是一个 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

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

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

另请参阅

适用于

ReadIntPtr(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
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 无效。

示例

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

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)复制到单独的托管数组的费用。

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

另请参阅

适用于