Marshal.ReadInt64 方法

定义

从非托管内存中读取一个 64 位带符号整数。 支持从未对齐的内存位置进行读取。

重载

ReadInt64(IntPtr)

从非托管内存中读取一个 64 位带符号整数。

ReadInt64(IntPtr, Int32)

从非托管内存按给定的偏移量读取一个 64 位带符号整数。

ReadInt64(Object, Int32)
已过时.

从非托管内存按给定的偏移量读取一个 64 位带符号整数。

ReadInt64(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

从非托管内存中读取一个 64 位带符号整数。

public:
 static long ReadInt64(IntPtr ptr);
[System.Security.SecurityCritical]
public static long ReadInt64 (IntPtr ptr);
public static long ReadInt64 (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadInt64 : nativeint -> int64
static member ReadInt64 : nativeint -> int64
Public Shared Function ReadInt64 (ptr As IntPtr) As Long

参数

ptr
IntPtr

nativeint

非托管内存中开始读取的地址。

返回

从非托管内存中读取的 64 位带符号整数。

属性

例外

ptr 不是识别的格式。

- 或 -

ptrnull

- 或 -

ptr 无效。

示例

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

static void ReadWriteInt64()
{
    // Allocate unmanaged memory. 
    int elementSize = 8;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt64(unmanagedArray, i * elementSize, ((Int64)(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.ReadInt64(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt64()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 8
    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.WriteInt64(unmanagedArray, i * elementSize, CType(i + 1, Int64))
    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.ReadInt64(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

下面的示例演示如何使用 ReadInt64 方法读取非托管 __int64 变量的值。

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



void main()
{
    // Create an unmanaged __int64.
    __int64 myVal = 42;

    // Read the value as a managed Int64.
    Int64 ^ myManagedVal = Marshal::ReadInt64((IntPtr) &myVal);

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

注解

ReadInt64 具有 0 的隐式偏移量。 此方法允许与非托管 C 样式 Int64 数组直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 复制到单独托管数组 (整个非托管数组的开销。

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

另请参阅

适用于

ReadInt64(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

从非托管内存按给定的偏移量读取一个 64 位带符号整数。

public:
 static long ReadInt64(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static long ReadInt64 (IntPtr ptr, int ofs);
public static long ReadInt64 (IntPtr ptr, int ofs);
[<System.Security.SecurityCritical>]
static member ReadInt64 : nativeint * int -> int64
static member ReadInt64 : nativeint * int -> int64
Public Shared Function ReadInt64 (ptr As IntPtr, ofs As Integer) As Long

参数

ptr
IntPtr

nativeint

非托管内存中开始读取的基址。

ofs
Int32

额外的字节偏移量,在读取前添加到 ptr 参数中。

返回

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

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。

示例

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

static void ReadWriteInt64()
{
    // Allocate unmanaged memory. 
    int elementSize = 8;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt64(unmanagedArray, i * elementSize, ((Int64)(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.ReadInt64(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt64()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 8
    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.WriteInt64(unmanagedArray, i * elementSize, CType(i + 1, Int64))
    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.ReadInt64(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

下面的示例演示如何使用 ReadInt64 方法读取非托管 __int64 变量的值。


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



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

    // Read value as a managed Int64.
    Int64 ^ myManagedVal = Marshal::ReadInt64((IntPtr) myVal, 0);

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

注解

ReadInt64 允许与非托管的 64 位有符号数组直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组的开销。

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

另请参阅

适用于

ReadInt64(Object, Int32)

Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs
Source:
Marshal.CoreCLR.cs

注意

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

从非托管内存按给定的偏移量读取一个 64 位带符号整数。

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

参数

ptr
Object

非托管内存中源对象的基址。

ofs
Int32

额外的字节偏移量,在读取前添加到 ptr 参数中。

返回

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

属性

例外

基址 (ptr) 加上偏移字节 (ofs) 可产生空或无效地址。

ptrArrayWithOffset 对象。 此方法不接受 ArrayWithOffset 参数。

注解

ReadInt64 允许与非托管的 64 位有符号数组直接交互,从而消除了在读取其元素值之前使用 Marshal.Copy) 将整个非托管数组 (复制到单独的托管数组的开销。

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

另请参阅

适用于