Marshal.WriteIntPtr 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个处理器本机大小的整数值写入非托管内存。 在 32 位系统上写入 32 位整数,在 64 位系统上写入 64 位整数。 支持写入未对齐的内存位置。
重载
WriteIntPtr(IntPtr, IntPtr) |
将一个处理器本机大小的整数值写入非托管内存。 |
WriteIntPtr(IntPtr, Int32, IntPtr) |
按指定的偏移量将一个处理器本机大小的整数值写入非托管内存。 |
WriteIntPtr(Object, Int32, IntPtr) |
已过时.
将一个处理器本机大小的整数值写入非托管内存。 |
WriteIntPtr(IntPtr, IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
将一个处理器本机大小的整数值写入非托管内存。
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
非托管内存中要写入的地址。
- val
-
IntPtr
nativeint
要写入的值。
- 属性
例外
示例
以下示例演示如何使用 ReadIntPtr 和 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
注解
WriteIntPtr 允许与非托管 C 样式 IntPtr
数组直接交互,从而消除了在设置其元素值之前使用 Marshal.Copy) 将整个非托管数组复制到单独托管数组 (的开销。
支持写入未对齐的内存位置。
另请参阅
适用于
WriteIntPtr(IntPtr, Int32, IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
按指定的偏移量将一个处理器本机大小的整数值写入非托管内存。
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
非托管内存中要写入的基址。
- ofs
- Int32
额外的字节偏移量,在写入前添加到 ptr
参数中。
- val
-
IntPtr
nativeint
要写入的值。
- 属性
例外
基址 (ptr
) 加上偏移字节 (ofs
) 可产生空或无效地址。
示例
以下示例演示如何使用 ReadIntPtr 和 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
注解
此方法在 32 位系统上写入 32 位整数,在 64 位系统上写入 64 位整数。
WriteIntPtr 允许与非托管 C 样式 IntPtr
数组直接交互,从而消除了在设置其元素值之前使用 Marshal.Copy) 将整个非托管数组复制到单独托管数组 (的开销。
支持写入未对齐的内存位置。
另请参阅
适用于
WriteIntPtr(Object, Int32, IntPtr)
- Source:
- Marshal.cs
- Source:
- Marshal.cs
- Source:
- Marshal.cs
注意
WriteIntPtr(Object, Int32, IntPtr) may be unavailable in future releases.
将一个处理器本机大小的整数值写入非托管内存。
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
非托管内存中目标对象的基址。
- ofs
- Int32
额外的字节偏移量,在写入前添加到 ptr
参数中。
- val
-
IntPtr
nativeint
要写入的值。
- 属性
例外
基址 (ptr
) 加上偏移字节 (ofs
) 可产生空或无效地址。
ptr
是 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。
注解
WriteIntPtr 启用与非托管 C 样式字节数组的直接交互,从而消除了在设置其元素值之前使用 Marshal.Copy) 复制到单独的托管数组 (整个非托管数组的开销。
支持写入未对齐的内存位置。