共用方式為


Marshal.ReadInt16 方法

定義

從 Unmanaged 記憶體讀取 16 位帶正負號的整數。 支援從未對齊的記憶體位置讀取。

多載

ReadInt16(IntPtr)

從 Unmanaged 記憶體讀取 16 位帶正負號的整數。

ReadInt16(IntPtr, Int32)

從 Unmanaged 記憶體的指定位移讀取 16 位帶正負號的整數。

ReadInt16(Object, Int32)
已淘汰.

從 Unmanaged 記憶體的指定位移讀取 16 位帶正負號的整數。

ReadInt16(IntPtr)

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

從 Unmanaged 記憶體讀取 16 位帶正負號的整數。

public:
 static short ReadInt16(IntPtr ptr);
[System.Security.SecurityCritical]
public static short ReadInt16 (IntPtr ptr);
public static short ReadInt16 (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadInt16 : nativeint -> int16
static member ReadInt16 : nativeint -> int16
Public Shared Function ReadInt16 (ptr As IntPtr) As Short

參數

ptr
IntPtr

nativeint

要從中讀取之 Unmanaged 記憶體中的位址。

傳回

從 Unmanaged 記憶體讀取的 16 位帶正負號的整數。

屬性

例外狀況

ptr 不是可辨識的格式。

-或-

ptr null

-或-

ptr 無效。

範例

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

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

下列範例示範如何使用 ReadInt16 方法來讀取 Unmanaged short 變數的值。


using namespace System;
using namespace System::Runtime::InteropServices;



void main()
{
    // Create an unmanaged short.
    short myShort = 42;

    // Read the short as a managed Int16.
        Int16 ^ myManagedVal = Marshal::ReadInt16((IntPtr) &myShort);

    // Display the value to the console.
    Console::WriteLine(myManagedVal);
}

備註

ReadInt16 隱含位移為 0。 此方法可讓您與 Unmanaged C 樣式的直接互動 Int16 陣列,而不需要先將整個 Unmanaged 陣列 (使用 Marshal.Copy) 複製到個別的 Managed 陣列,再讀取其元素值。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於

ReadInt16(IntPtr, Int32)

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

從 Unmanaged 記憶體的指定位移讀取 16 位帶正負號的整數。

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

參數

ptr
IntPtr

nativeint

要從中讀取之 Unmanaged 記憶體中的基位址。

ofs
Int32

額外的位元組位移,會在讀取之前新增至 ptr 參數。

傳回

從指定位移的 Unmanaged 記憶體讀取的 16 位帶正負號的整數。

屬性

例外狀況

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

範例

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

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

下列範例示範如何使用 ReadInt16 方法來讀取 Unmanaged short 變數的值。


using namespace System;
using namespace System::Runtime::InteropServices;

void main()
{
    // Create an unmanaged short pointer.
    short * myShort;
    short tmp = 42;
    // Initialize it to another value.
    myShort = &tmp;

    // Read value as a managed Int16.
    Int16 ^ myManagedVal = Marshal::ReadInt16((IntPtr) myShort, 0);

    // Display the value to the console.
    Console::WriteLine(myManagedVal);
}

備註

ReadInt16 可讓您與 Unmanaged 16 位帶正負號陣列的直接互動,而不需要先將整個 Unmanaged 陣組(使用 Marshal.Copy)複製到個別的 Managed 陣組,再讀取其元素值。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於

ReadInt16(Object, Int32)

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

警告

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

從 Unmanaged 記憶體的指定位移讀取 16 位帶正負號的整數。

public:
 static short ReadInt16(System::Object ^ ptr, int ofs);
[System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static short ReadInt16 (object ptr, int ofs);
[System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")]
public static short ReadInt16 (object ptr, int ofs);
public static short ReadInt16 (object ptr, int ofs);
[System.Security.SecurityCritical]
public static short ReadInt16 (object ptr, int ofs);
[<System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member ReadInt16 : obj * int -> int16
[<System.Obsolete("ReadInt16(Object, Int32) may be unavailable in future releases.")>]
static member ReadInt16 : obj * int -> int16
static member ReadInt16 : obj * int -> int16
[<System.Security.SecurityCritical>]
static member ReadInt16 : obj * int -> int16
Public Shared Function ReadInt16 (ptr As Object, ofs As Integer) As Short

參數

ptr
Object

來源物件之 Unmanaged 記憶體中的基位址。

ofs
Int32

額外的位元組位移,會在讀取之前新增至 ptr 參數。

傳回

從指定位移的 Unmanaged 記憶體讀取的 16 位帶正負號的整數。

屬性

例外狀況

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

ptrArrayWithOffset 物件。 此方法不接受 ArrayWithOffset 參數。

備註

ReadInt16 可讓您與 Unmanaged 16 位帶正負號陣列的直接互動,而不需要先將整個 Unmanaged 陣組(使用 Marshal.Copy)複製到個別的 Managed 陣組,再讀取其元素值。

支援從未對齊的記憶體位置讀取。

另請參閱

適用於