共用方式為


Marshal.ReadInt64 方法

定義

從 Unmanaged 記憶體讀取 64 位元帶正負號的整數。 支援讀取未配置的記憶體位置。

多載

ReadInt64(IntPtr)

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

ReadInt64(IntPtr, Int32)

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

ReadInt64(Object, Int32)
已淘汰.

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

ReadInt64(IntPtr)

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

從 Unmanaged 記憶體讀取 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

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

傳回

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

屬性

例外狀況

ptr 不是可辨認的格式。

-或-

ptrnull

-或-

ptr 無效。

範例

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

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 方法來讀取 Unmanaged __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 的隱含位移。 這個方法可讓您直接與 Unmanaged C 樣式 Int64 數位互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 陣組複製 () (。

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

另請參閱

適用於

ReadInt64(IntPtr, Int32)

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

從 Unmanaged 記憶體中指定位移處讀取 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

從 Unmanaged 記憶體中讀取的基底位址 (Base Address)。

ofs
Int32

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

傳回

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

屬性

例外狀況

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

範例

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

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 方法來讀取 Unmanaged __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 可讓您直接與 Unmanaged 64 位帶正負號的陣列互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 數位複製 () (的費用。

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

另請參閱

適用於

ReadInt64(Object, Int32)

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

警告

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

從 Unmanaged 記憶體中指定位移處讀取 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

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

ofs
Int32

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

傳回

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

屬性

例外狀況

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

ptrArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。

備註

ReadInt64 可讓您直接與 Unmanaged 64 位帶正負號的陣列互動,避免在讀取其元素值之前,先將 Marshal.Copy 整個 Unmanaged 數位複製 () (的費用。

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

另請參閱

適用於