Marshal.ReadInt16 方法

定义

从非托管内存中读取 16 位有符号整数。 支持从未对齐的内存位置读取。

重载

ReadInt16(IntPtr)

从非托管内存中读取 16 位有符号整数。

ReadInt16(IntPtr, Int32)

从非托管内存的给定偏移量读取 16 位有符号整数。

ReadInt16(Object, Int32)
已过时.

从非托管内存的给定偏移量读取 16 位有符号整数。

ReadInt16(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
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 不是可识别的格式。

-或-

ptr null

-或-

ptr 无效。

示例

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

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)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
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 或无效地址。

示例

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

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)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
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 或无效地址。

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

注解

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

支持从未对齐的内存位置读取。

另请参阅

适用于