Bewerken

Delen via


Marshal.ReadInt64 Method

Definition

Reads a 64-bit signed integer from unmanaged memory. Reading from unaligned memory locations is supported.

Overloads

ReadInt64(IntPtr)

Reads a 64-bit signed integer from unmanaged memory.

ReadInt64(IntPtr, Int32)

Reads a 64-bit signed integer at a given offset from unmanaged memory.

ReadInt64(Object, Int32)
Obsolete.

Reads a 64-bit signed integer at a given offset from unmanaged memory.

ReadInt64(IntPtr)

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

Reads a 64-bit signed integer from unmanaged memory.

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

Parameters

ptr
IntPtr

nativeint

The address in unmanaged memory from which to read.

Returns

The 64-bit signed integer read from unmanaged memory.

Attributes

Exceptions

ptr is not a recognized format.

-or-

ptr is null.

-or-

ptr is invalid.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadInt64 and WriteInt64 methods.

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

The following example demonstrates how to use the ReadInt64 method to read the value of an unmanaged __int64 variable.

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);
}

Remarks

ReadInt64 has an implied offset of 0. This method enables direct interaction with an unmanaged C-style Int64 array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

ReadInt64(IntPtr, Int32)

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

Reads a 64-bit signed integer at a given offset from unmanaged memory.

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

Parameters

ptr
IntPtr

nativeint

The base address in unmanaged memory from which to read.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before reading.

Returns

The 64-bit signed integer read from unmanaged memory at the given offset.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

Examples

The following example demonstrates how to read and write to an unmanaged array using the ReadInt64 and WriteInt64 methods.

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

The following example demonstrates how to use the ReadInt64 method to read the value of an unmanaged __int64 variable.


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);
}

Remarks

ReadInt64 enables direct interaction with an unmanaged 64-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to

ReadInt64(Object, Int32)

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

Caution

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

Reads a 64-bit signed integer at a given offset from unmanaged memory.

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

Parameters

ptr
Object

The base address in unmanaged memory of the source object.

ofs
Int32

An additional byte offset, which is added to the ptr parameter before reading.

Returns

The 64-bit signed integer read from unmanaged memory at the given offset.

Attributes

Exceptions

Base address (ptr) plus offset byte (ofs) produces a null or invalid address.

ptr is an ArrayWithOffset object. This method does not accept ArrayWithOffset parameters.

Remarks

ReadInt64 enables direct interaction with an unmanaged 64-bit signed array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.

Reading from unaligned memory locations is supported.

See also

Applies to