Marshal.WriteInt16 方法

定义

将 16 位有符号整数值写入非托管内存。 支持写入未对齐的内存位置。

重载

名称 说明
WriteInt16(IntPtr, Char)

将字符作为 16 位整数值写入非托管内存。

WriteInt16(IntPtr, Int16)

将 16 位整数值写入非托管内存。

WriteInt16(IntPtr, Int32, Char)

将 16 位有符号整数值写入指定偏移量的非托管内存。

WriteInt16(IntPtr, Int32, Int16)

将 16 位有符号整数值写入非托管内存中的指定偏移量。

WriteInt16(Object, Int32, Char)
已过时.

将 16 位有符号整数值写入指定偏移量的非托管内存。

WriteInt16(Object, Int32, Int16)
已过时.

将 16 位有符号整数值写入指定偏移量的非托管内存。

WriteInt16(IntPtr, Char)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

将字符作为 16 位整数值写入非托管内存。

public:
 static void WriteInt16(IntPtr ptr, char val);
[System.Security.SecurityCritical]
public static void WriteInt16(IntPtr ptr, char val);
public static void WriteInt16(IntPtr ptr, char val);
[<System.Security.SecurityCritical>]
static member WriteInt16 : nativeint * char -> unit
static member WriteInt16 : nativeint * char -> unit
Public Shared Sub WriteInt16 (ptr As IntPtr, val As Char)

参数

ptr
IntPtr

nativeint

要写入的非托管内存中的地址。

val
Char

要写入的值。

属性

例外

ptr 不是可识别的格式。

-或-

ptrnull

-或-

ptr 无效。

示例

以下示例演示如何使用 ReadInt16 and WriteInt16 方法读取和写入非托管数组。

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    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.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    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.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于

WriteInt16(IntPtr, Int16)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

将 16 位整数值写入非托管内存。

public:
 static void WriteInt16(IntPtr ptr, short val);
[System.Security.SecurityCritical]
public static void WriteInt16(IntPtr ptr, short val);
public static void WriteInt16(IntPtr ptr, short val);
[<System.Security.SecurityCritical>]
static member WriteInt16 : nativeint * int16 -> unit
static member WriteInt16 : nativeint * int16 -> unit
Public Shared Sub WriteInt16 (ptr As IntPtr, val As Short)

参数

ptr
IntPtr

nativeint

要写入的非托管内存中的地址。

val
Int16

要写入的值。

属性

例外

ptr 不是可识别的格式。

-或-

ptrnull

-或-

ptr 无效。

示例

以下示例演示如何使用 ReadInt16 and WriteInt16 方法读取和写入非托管数组。

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    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.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    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.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于

WriteInt16(IntPtr, Int32, Char)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

将 16 位有符号整数值写入指定偏移量的非托管内存。

public:
 static void WriteInt16(IntPtr ptr, int ofs, char val);
[System.Security.SecurityCritical]
public static void WriteInt16(IntPtr ptr, int ofs, char val);
public static void WriteInt16(IntPtr ptr, int ofs, char val);
[<System.Security.SecurityCritical>]
static member WriteInt16 : nativeint * int * char -> unit
static member WriteInt16 : nativeint * int * char -> unit
Public Shared Sub WriteInt16 (ptr As IntPtr, ofs As Integer, val As Char)

参数

ptr
IntPtr

nativeint

要写入的本机堆中的基址。

ofs
Int32

另一个字节偏移量,该偏移量在写入前添加到 ptr 参数中。

val
Char

要写入的值。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 生成 null 或无效地址。

示例

以下示例演示如何使用 ReadInt16 and WriteInt16 方法读取和写入非托管数组。

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    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.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    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.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于

WriteInt16(IntPtr, Int32, Int16)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

将 16 位有符号整数值写入非托管内存中的指定偏移量。

public:
 static void WriteInt16(IntPtr ptr, int ofs, short val);
[System.Security.SecurityCritical]
public static void WriteInt16(IntPtr ptr, int ofs, short val);
public static void WriteInt16(IntPtr ptr, int ofs, short val);
[<System.Security.SecurityCritical>]
static member WriteInt16 : nativeint * int * int16 -> unit
static member WriteInt16 : nativeint * int * int16 -> unit
Public Shared Sub WriteInt16 (ptr As IntPtr, ofs As Integer, val As Short)

参数

ptr
IntPtr

nativeint

要写入的非托管内存中的基址。

ofs
Int32

另一个字节偏移量,该偏移量在写入前添加到 ptr 参数中。

val
Int16

要写入的值。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 生成 null 或无效地址。

示例

以下示例演示如何使用 ReadInt16 and WriteInt16 方法读取和写入非托管数组。

static void ReadWriteInt16()
{
    // Allocate unmanaged memory. 
    int elementSize = 2;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt16()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 2
    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.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
    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.ReadInt16(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于

WriteInt16(Object, Int32, Char)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

注意

WriteInt16(Object, Int32, Char) may be unavailable in future releases.

将 16 位有符号整数值写入指定偏移量的非托管内存。

public:
 static void WriteInt16(System::Object ^ ptr, int ofs, char val);
[System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt16(object ptr, int ofs, char val);
[System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt16(object ptr, int ofs, char val);
[System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")]
public static void WriteInt16(object ptr, int ofs, char val);
public static void WriteInt16(object ptr, int ofs, char val);
[System.Security.SecurityCritical]
public static void WriteInt16(object ptr, int ofs, char val);
[<System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member WriteInt16 : obj * int * char -> unit
[<System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
static member WriteInt16 : obj * int * char -> unit
[<System.Obsolete("WriteInt16(Object, Int32, Char) may be unavailable in future releases.")>]
static member WriteInt16 : obj * int * char -> unit
static member WriteInt16 : obj * int * char -> unit
[<System.Security.SecurityCritical>]
static member WriteInt16 : obj * int * char -> unit
Public Shared Sub WriteInt16 (ptr As Object, ofs As Integer, val As Char)

参数

ptr
Object

目标对象的非托管内存中的基址。

ofs
Int32

另一个字节偏移量,该偏移量在写入前添加到 ptr 参数中。

val
Char

要写入的值。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 生成 null 或无效地址。

ptr 是一个 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于

WriteInt16(Object, Int32, Int16)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

注意

WriteInt16(Object, Int32, Int16) may be unavailable in future releases.

将 16 位有符号整数值写入指定偏移量的非托管内存。

public:
 static void WriteInt16(System::Object ^ ptr, int ofs, short val);
[System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt16(object ptr, int ofs, short val);
[System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt16(object ptr, int ofs, short val);
[System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")]
public static void WriteInt16(object ptr, int ofs, short val);
public static void WriteInt16(object ptr, int ofs, short val);
[System.Security.SecurityCritical]
public static void WriteInt16(object ptr, int ofs, short val);
[<System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member WriteInt16 : obj * int * int16 -> unit
[<System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Marshalling code for the object might not be available")>]
static member WriteInt16 : obj * int * int16 -> unit
[<System.Obsolete("WriteInt16(Object, Int32, Int16) may be unavailable in future releases.")>]
static member WriteInt16 : obj * int * int16 -> unit
static member WriteInt16 : obj * int * int16 -> unit
[<System.Security.SecurityCritical>]
static member WriteInt16 : obj * int * int16 -> unit
Public Shared Sub WriteInt16 (ptr As Object, ofs As Integer, val As Short)

参数

ptr
Object

目标对象的非托管内存中的基址。

ofs
Int32

另一个字节偏移量,该偏移量在写入前添加到 ptr 参数中。

val
Int16

要写入的值。

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 生成 null 或无效地址。

ptr 是一个 ArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

WriteInt16 启用与非托管 16 位带符号数组的直接交互,从而消除了在设置其元素值之前将整个非托管数组(using Marshal.Copy)复制到单独的托管数组的费用。

支持写入未对齐的内存位置。

另请参阅

适用于