Marshal.ReadInt16 メソッド

定義

アンマネージ メモリから 16 ビット符号付き整数を読み取ります。 アライメントされていないメモリ位置からの読み取りはサポートされています。

オーバーロード

ReadInt16(IntPtr)

アンマネージ メモリから 16 ビット符号付き整数を読み取ります。

ReadInt16(IntPtr, Int32)

アンマネージ メモリの指定されたオフセットから 16 ビット符号付き整数を読み取ります。

ReadInt16(Object, Int32)
古い.

アンマネージ メモリの指定されたオフセットから 16 ビット符号付き整数を読み取ります。

ReadInt16(IntPtr)

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージ メモリから 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

読み込み元となるアンマネージ メモリ内のアドレス。

戻り値

アンマネージ メモリから読み取られた 16 ビット符号付き整数。

属性

例外

ptr が認識された形式ではありません。

または

ptrnull です。

または

ptr が無効です。

次の例では、 メソッドと WriteInt16 メソッドを使用してアンマネージド配列の読み取りと書き込みを行う方法を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 してアンマネージ 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 です。 このメソッドを使用すると、アンマネージ C スタイル Int16 の配列と直接やり取りできるため、アンマネージ配列全体 (を使用) Marshal.Copyを別のマネージド配列にコピーしてから要素値を読み取る必要がなくなります。

アライメントされていないメモリ位置からの読み取りはサポートされています。

こちらもご覧ください

適用対象

ReadInt16(IntPtr, Int32)

ソース:
Marshal.cs
ソース:
Marshal.cs
ソース:
Marshal.cs

アンマネージ メモリの指定されたオフセットから 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

読み込み元となるアンマネージ メモリ内のベース アドレス。

ofs
Int32

読み取りの前に ptr パラメーターに追加される追加のバイト オフセット。

戻り値

アンマネージ メモリの指定されたオフセットから読み取られた 16 ビット符号付き整数。

属性

例外

ベース アドレス (ptr) にオフセット バイト (ofs) を足すと、null または無効なアドレスが生成されます。

次の例では、 メソッドと WriteInt16 メソッドを使用してアンマネージド配列の読み取りと書き込みを行う方法を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 してアンマネージ 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 を使用すると、アンマネージ 16 ビット符号付き配列と直接やり取りできるため、アンマネージ配列全体 (を使用) Marshal.Copyを別のマネージド配列にコピーしてから要素の値を読み取る必要がなくなります。

アライメントされていないメモリ位置からの読み取りはサポートされています。

こちらもご覧ください

適用対象

ReadInt16(Object, Int32)

ソース:
Marshal.CoreCLR.cs
ソース:
Marshal.CoreCLR.cs
ソース:
Marshal.CoreCLR.cs

注意事項

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

アンマネージ メモリの指定されたオフセットから 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

ソース オブジェクトのアンマネージ メモリ内のベース アドレス。

ofs
Int32

読み取りの前に ptr パラメーターに追加される追加のバイト オフセット。

戻り値

アンマネージ メモリの指定されたオフセットから読み取られた 16 ビット符号付き整数。

属性

例外

ベース アドレス (ptr) にオフセット バイト (ofs) を足すと、null または無効なアドレスが生成されます。

ptrArrayWithOffset オブジェクトです。 このメソッドは、ArrayWithOffset パラメーターを受け入れません。

注釈

ReadInt16 を使用すると、アンマネージ 16 ビット符号付き配列と直接やり取りできるため、アンマネージ配列全体 (を使用) Marshal.Copyを別のマネージド配列にコピーしてから要素の値を読み取る必要がなくなります。

アライメントされていないメモリ位置からの読み取りはサポートされています。

こちらもご覧ください

適用対象