Marshal.ReadInt32 メソッド

定義

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

オーバーロード

ReadInt32(IntPtr)

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

ReadInt32(IntPtr, Int32)

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

ReadInt32(Object, Int32)
古い.

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

ReadInt32(IntPtr)

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

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

public:
 static int ReadInt32(IntPtr ptr);
[System.Security.SecurityCritical]
public static int ReadInt32 (IntPtr ptr);
public static int ReadInt32 (IntPtr ptr);
[<System.Security.SecurityCritical>]
static member ReadInt32 : nativeint -> int
static member ReadInt32 : nativeint -> int
Public Shared Function ReadInt32 (ptr As IntPtr) As Integer

パラメーター

ptr
IntPtr

nativeint

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

戻り値

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

属性

例外

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

または

ptrnullです。

または

ptr が無効です。

次の例では、 メソッドWriteInt32と メソッドを使用してアンマネージド配列の読み取りと書き込みを行う方法をReadInt32示します。

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

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

    Marshal.FreeHGlobal(unmanagedArray);

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

    Marshal.FreeHGlobal(unmanagedArray)

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

次の例では、 メソッドを使用 ReadInt32 してアンマネージ int 変数の値を読み取る方法を示します。

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



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

    // Read the int as a managed Int32.
        Int32 ^ myManagedVal = Marshal::ReadInt32((IntPtr) &myVal);

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

注釈

ReadInt32 の暗黙のオフセットは 0 です。 このメソッドを使用すると、アンマネージド C スタイル Int32 の配列と直接やり取りできるため、要素値を読み取る前にアンマネージド配列全体 (を使用 Marshal.Copy) を別のマネージド配列にコピーする手間を省きます。

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

こちらもご覧ください

適用対象

ReadInt32(IntPtr, Int32)

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

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

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

パラメーター

ptr
IntPtr

nativeint

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

ofs
Int32

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

戻り値

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

属性

例外

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

次の例では、 メソッドWriteInt32と メソッドを使用してアンマネージド配列の読み取りと書き込みを行う方法をReadInt32示します。

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

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

    Marshal.FreeHGlobal(unmanagedArray);

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

    Marshal.FreeHGlobal(unmanagedArray)

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

次の例では、 メソッドを使用 ReadInt32 してアンマネージ int 変数の値を読み取る方法を示します。

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



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

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

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

注釈

ReadInt32 を使用すると、アンマネージド 32 ビット符号付き配列との直接の対話が可能になり、要素値を読み取る前にアンマネージド配列全体 (を使用 Marshal.Copy) を別のマネージド配列にコピーする手間を省きます。

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

こちらもご覧ください

適用対象

ReadInt32(Object, Int32)

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

注意事項

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

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

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

パラメーター

ptr
Object

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

ofs
Int32

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

戻り値

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

属性

例外

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

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

注釈

ReadInt32 を使用すると、アンマネージド 32 ビット符号付き配列との直接の対話が可能になり、要素値を読み取る前にアンマネージド配列全体 (を使用 Marshal.Copy) を別のマネージド配列にコピーする手間を省きます。

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

こちらもご覧ください

適用対象