Marshal.ReadInt32 メソッド

定義

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

オーバーロード

名前 説明
ReadInt32(IntPtr)

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

ReadInt32(IntPtr, Int32)

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

ReadInt32(Object, Int32)
古い.

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

ReadInt32(IntPtr)

アンマネージ メモリから 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 が無効です。

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

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)

アンマネージ メモリから、指定されたオフセットで 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 または無効なアドレスを生成します。

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

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)

注意事項

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);
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.")]
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
static member ReadInt32 : obj * int -> int
[<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
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 を使用して) アンマネージド配列全体を別のマネージド配列にコピーする手間が省けます。

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

こちらもご覧ください

適用対象