Marshal.ReadInt32 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从非托管内存中读取一个 32 位带符号整数。 支持从未对齐的内存位置进行读取。
重载
ReadInt32(IntPtr) |
从非托管内存中读取一个 32 位带符号整数。 |
ReadInt32(IntPtr, Int32) |
从非托管内存按给定的偏移量读取一个 32 位带符号整数。 |
ReadInt32(Object, Int32) |
已过时.
从非托管内存按给定的偏移量读取一个 32 位带符号整数。 |
ReadInt32(IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
从非托管内存中读取一个 32 位带符号整数。
public:
static int ReadInt32(IntPtr ptr);
[System.Security.SecurityCritical]
public static int ReadInt32 (IntPtr ptr);
public static int ReadInt32 (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadInt32 : nativeint -> int
static member ReadInt32 : nativeint -> int
Public Shared Function ReadInt32 (ptr As IntPtr) As Integer
参数
- ptr
-
IntPtr
nativeint
非托管内存中开始读取的地址。
返回
从非托管内存中读取的 32 位带符号整数。
- 属性
例外
示例
以下示例演示如何使用 ReadInt32 和 WriteInt32 方法读取和写入非托管数组。
static void ReadWriteInt32()
{
// Allocate unmanaged memory.
int elementSize = 4;
IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);
// Set the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
{
Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(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.ReadInt32(unmanagedArray, i * elementSize));
}
Marshal.FreeHGlobal(unmanagedArray);
Console.WriteLine("Done. Press Enter to continue.");
Console.ReadLine();
}
Sub ReadWriteInt32()
' Allocate unmanaged memory.
Dim elementSize As Integer = 4
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.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
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.ReadInt32(unmanagedArray, i * elementSize))
Next i
Marshal.FreeHGlobal(unmanagedArray)
Console.WriteLine("Done. Press Enter to continue.")
Console.ReadLine()
End Sub
下面的示例演示如何使用 ReadInt32 方法读取非托管 int
变量的值。
using namespace System;
using namespace System::Runtime::InteropServices;
void main()
{
// Create an unmanaged integer.
int myVal = 42;
// Read the int as a managed Int32.
Int32 ^ myManagedVal = Marshal::ReadInt32((IntPtr) &myVal);
// Display the value to the console.
Console::WriteLine(myManagedVal);
}
注解
ReadInt32 具有 0 的隐式偏移量。 此方法允许与非托管 C 样式 Int32
数组直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。
支持从未对齐的内存位置进行读取。
另请参阅
适用于
ReadInt32(IntPtr, Int32)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
从非托管内存按给定的偏移量读取一个 32 位带符号整数。
public:
static int ReadInt32(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static int ReadInt32 (IntPtr ptr, int ofs);
public static int ReadInt32 (IntPtr ptr, int ofs);
[<System.Security.SecurityCritical>]
static member ReadInt32 : nativeint * int -> int
static member ReadInt32 : nativeint * int -> int
Public Shared Function ReadInt32 (ptr As IntPtr, ofs As Integer) As Integer
参数
- ptr
-
IntPtr
nativeint
非托管内存中开始读取的基址。
- ofs
- Int32
额外的字节偏移量,在读取前添加到 ptr
参数中。
返回
从非托管内存中读取的 32 位带符号整数。
- 属性
例外
基址 (ptr
) 加上偏移字节 (ofs
) 可产生空或无效地址。
示例
以下示例演示如何使用 ReadInt32 和 WriteInt32 方法读取和写入非托管数组。
static void ReadWriteInt32()
{
// Allocate unmanaged memory.
int elementSize = 4;
IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);
// Set the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
{
Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(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.ReadInt32(unmanagedArray, i * elementSize));
}
Marshal.FreeHGlobal(unmanagedArray);
Console.WriteLine("Done. Press Enter to continue.");
Console.ReadLine();
}
Sub ReadWriteInt32()
' Allocate unmanaged memory.
Dim elementSize As Integer = 4
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.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
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.ReadInt32(unmanagedArray, i * elementSize))
Next i
Marshal.FreeHGlobal(unmanagedArray)
Console.WriteLine("Done. Press Enter to continue.")
Console.ReadLine()
End Sub
下面的示例演示如何使用 ReadInt32 方法读取非托管 int
变量的值。
using namespace System;
using namespace System::Runtime::InteropServices;
void main()
{
// Create an unmanaged int pointer.
int * myVal;
int tmp = 42;
// Initialize it to another value.
myVal = &tmp;
// Read value as a managed Int32.
Int32 ^ myManagedVal = Marshal::ReadInt32((IntPtr) myVal, 0);
// Display the value to the console.
Console::WriteLine(myManagedVal);
}
注解
ReadInt32 支持与非托管 32 位带符号数组的直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。
支持从未对齐的内存位置进行读取。
另请参阅
适用于
ReadInt32(Object, Int32)
- Source:
- Marshal.CoreCLR.cs
- Source:
- Marshal.CoreCLR.cs
- Source:
- Marshal.CoreCLR.cs
注意
ReadInt32(Object, Int32) may be unavailable in future releases.
从非托管内存按给定的偏移量读取一个 32 位带符号整数。
public:
static int ReadInt32(System::Object ^ ptr, int ofs);
[System.Obsolete("ReadInt32(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static int ReadInt32 (object ptr, int ofs);
[System.Obsolete("ReadInt32(Object, Int32) may be unavailable in future releases.")]
public static int ReadInt32 (object ptr, int ofs);
public static int ReadInt32 (object ptr, int ofs);
[System.Security.SecurityCritical]
public static int ReadInt32 (object ptr, int ofs);
[<System.Obsolete("ReadInt32(Object, Int32) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member ReadInt32 : obj * int -> int
[<System.Obsolete("ReadInt32(Object, Int32) may be unavailable in future releases.")>]
static member ReadInt32 : obj * int -> int
static member ReadInt32 : obj * int -> int
[<System.Security.SecurityCritical>]
static member ReadInt32 : obj * int -> int
Public Shared Function ReadInt32 (ptr As Object, ofs As Integer) As Integer
参数
- ptr
- Object
非托管内存中源对象的基址。
- ofs
- Int32
额外的字节偏移量,在读取前添加到 ptr
参数中。
返回
从非托管内存按给定的偏移量读取的 32 位带符号整数。
- 属性
例外
基址 (ptr
) 加上偏移字节 (ofs
) 可产生空或无效地址。
ptr
是 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。
注解
ReadInt32 支持与非托管 32 位带符号数组的直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。
支持从未对齐的内存位置进行读取。