Share via


Marshal.WriteInt16 方法

定義

將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體中。 支援寫入未配置的記憶體位置。

多載

WriteInt16(IntPtr, Char)

將字元當做 16 位元整數值寫入 Unmanaged 記憶體中。

WriteInt16(IntPtr, Int16)

將 16 位元整數值寫入 Unmanaged 記憶體中。

WriteInt16(IntPtr, Int32, Char)

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

WriteInt16(IntPtr, Int32, Int16)

將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體中的指定位移。

WriteInt16(Object, Int32, Char)
已淘汰.

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

WriteInt16(Object, Int32, Int16)
已淘汰.

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

WriteInt16(IntPtr, Char)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將字元當做 16 位元整數值寫入 Unmanaged 記憶體中。

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

Unmanaged 記憶體中要寫入的位址。

val
Char

要寫入的值。

屬性

例外狀況

ptr 不是可辨認的格式。

-或-

ptrnull

-或-

ptr 無效。

範例

下列範例示範如何使用 和 WriteInt16 方法來讀取和寫入 Unmanaged 陣列ReadInt16

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 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt16(IntPtr, Int16)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將 16 位元整數值寫入 Unmanaged 記憶體中。

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

Unmanaged 記憶體中要寫入的位址。

val
Int16

要寫入的值。

屬性

例外狀況

ptr 不是可辨認的格式。

-或-

ptrnull

-或-

ptr 無效。

範例

下列範例示範如何使用 和 WriteInt16 方法來讀取和寫入 Unmanaged 陣列ReadInt16

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 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt16(IntPtr, Int32, Char)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

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 或無效的位址。

範例

下列範例示範如何使用 和 WriteInt16 方法來讀取和寫入 Unmanaged 陣列ReadInt16

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 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt16(IntPtr, Int32, Int16)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體中的指定位移。

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

Unmanaged 記憶體中要寫入的基底位址。

ofs
Int32

額外的位元組位移,會先加入至參數 ptr,然後再進行寫入。

val
Int16

要寫入的值。

屬性

例外狀況

基底位址 (ptr) 加上位移位元組 (ofs) 會產生 Null 或無效的位址。

範例

下列範例示範如何使用 和 WriteInt16 方法來讀取和寫入 Unmanaged 陣列ReadInt16

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 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt16(Object, Int32, Char)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

警告

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

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

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.")]
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.")>]
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

目標物件之 Unmanaged 記憶體中的基底位址。

ofs
Int32

額外的位元組位移,會先加入至參數 ptr,然後再進行寫入。

val
Char

要寫入的值。

屬性

例外狀況

基底位址 (ptr) 加上位移位元組 (ofs) 會產生 Null 或無效的位址。

ptrArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。

備註

WriteInt16 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt16(Object, Int32, Int16)

來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs

警告

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

在指定的位移,將 16 位元帶正負號的整數值寫入 Unmanaged 記憶體。

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.")]
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.")>]
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

目標物件之 Unmanaged 記憶體中的基底位址。

ofs
Int32

額外的位元組位移,會先加入至參數 ptr,然後再進行寫入。

val
Int16

要寫入的值。

屬性

例外狀況

基底位址 (ptr) 加上位移位元組 (ofs) 會產生 Null 或無效的位址。

ptrArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。

備註

WriteInt16 可讓您直接與 Unmanaged 16 位帶正負號的 (陣列互動,避免使用 Marshal.Copy) 將整個 Unmanaged 陣組複製到個別 Managed 陣組,再設定其專案值。

支援寫入未配置的記憶體位置。

另請參閱

適用於