Marshal.ReadInt32 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 Unmanaged 記憶體讀取 32 位元帶正負號的整數。 支援讀取未配置的記憶體位置。
多載
ReadInt32(IntPtr) |
從 Unmanaged 記憶體讀取 32 位元帶正負號的整數。 |
ReadInt32(IntPtr, Int32) |
從 Unmanaged 記憶體中指定位移處讀取 32 位元帶正負號的整數。 |
ReadInt32(Object, Int32) |
已淘汰.
從 Unmanaged 記憶體中指定位移處讀取 32 位元帶正負號的整數。 |
ReadInt32(IntPtr)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
從 Unmanaged 記憶體讀取 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
從 Unmanaged 記憶體中讀取的位址。
傳回
從 Unmanaged 記憶體讀取的 32 位元帶正負號的整數。
- 屬性
例外狀況
範例
下列範例示範如何使用 和 WriteInt32 方法來讀取和寫入 Unmanaged 陣列ReadInt32。
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 方法來讀取 Unmanaged 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 的隱含位移。 這個方法可讓您直接與 Unmanaged C 樣式 Int32
數位互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 陣組複製 () (。
支援讀取未配置的記憶體位置。
另請參閱
適用於
ReadInt32(IntPtr, Int32)
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
- 來源:
- Marshal.cs
從 Unmanaged 記憶體中指定位移處讀取 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
從 Unmanaged 記憶體中讀取的基底位址 (Base Address)。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行讀取。
傳回
從 Unmanaged 記憶體讀取的 32 位元帶正負號的整數。
- 屬性
例外狀況
基底位址 (ptr
) 加上位移位元組 (ofs
) 會產生 Null 或無效的位址。
範例
下列範例示範如何使用 和 WriteInt32 方法來讀取和寫入 Unmanaged 陣列ReadInt32。
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 方法來讀取 Unmanaged 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 可讓您直接與 Unmanaged 32 位帶正負號的陣列互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 數位複製 () (。
支援讀取未配置的記憶體位置。
另請參閱
適用於
ReadInt32(Object, Int32)
警告
ReadInt32(Object, Int32) may be unavailable in future releases.
從 Unmanaged 記憶體中指定位移處讀取 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
來源物件之 Unmanaged 記憶體中的基底位址。
- ofs
- Int32
額外的位元組位移,會先加入至參數 ptr
,然後再進行讀取。
傳回
從 Unmanaged 記憶體中指定位移處讀取的 32 位元帶正負號的整數。
- 屬性
例外狀況
基底位址 (ptr
) 加上位移位元組 (ofs
) 會產生 Null 或無效的位址。
ptr
是 ArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。
備註
ReadInt32 可讓您直接與 Unmanaged 32 位帶正負號的陣列互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 數位複製 () (。
支援讀取未配置的記憶體位置。