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