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