共用方式為


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 不是可辨認的格式。

-或-

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

下列範例示範如何使用 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 數位互動,避免在讀取其元素值之前,先) 將 Marshal.Copy 整個 Unmanaged 陣組 (複製整個非受控數位列 (。

支援讀取未配置的記憶體位置。

另請參閱

適用於

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 記憶體中讀取的基底位址 (Base Address)。

ofs
Int32

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

傳回

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

屬性

例外狀況

基底位址 (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

下列範例示範如何使用 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 位帶正負號的陣列互動,而不需要先使用 Marshal.Copy) 將整個 Unmanaged (陣組複製到個別 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 位帶正負號的陣列互動,而不需要先使用 Marshal.Copy) 將整個 Unmanaged (陣組複製到個別 Managed 陣列,再讀取其元素值。

支援讀取未配置的記憶體位置。

另請參閱

適用於