Marshal.ReadInt32 Método
Definição
Lê um inteiro com sinal de 32 bits da memória não gerenciada.Reads a 32-bit signed integer from unmanaged memory. Há suporte para leitura de locais de memória desalinhados.Reading from unaligned memory locations is supported.
Sobrecargas
ReadInt32(IntPtr) |
Lê um inteiro com sinal de 32 bits da memória não gerenciada.Reads a 32-bit signed integer from unmanaged memory. |
ReadInt32(IntPtr, Int32) |
Lê um inteiro com sinal de 32 bits em um deslocamento fornecido na memória não gerenciada.Reads a 32-bit signed integer at a given offset from unmanaged memory. |
ReadInt32(Object, Int32) |
Lê um inteiro com sinal de 32 bits em um deslocamento fornecido na memória não gerenciada.Reads a 32-bit signed integer at a given offset from unmanaged memory. |
ReadInt32(IntPtr)
Lê um inteiro com sinal de 32 bits da memória não gerenciada.Reads a 32-bit signed integer from unmanaged memory.
public:
static int ReadInt32(IntPtr ptr);
[System.Security.SecurityCritical]
public static int ReadInt32 (IntPtr ptr);
static member ReadInt32 : nativeint -> int
Public Shared Function ReadInt32 (ptr As IntPtr) As Integer
Parâmetros
- ptr
- IntPtr
O endereço na memória não gerenciada do qual ler.The address in unmanaged memory from which to read.
Retornos
O inteiro com sinal de 32 bits da memória não gerenciada.The 32-bit signed integer read from unmanaged memory.
- Atributos
Exceções
ptr
não é um formato reconhecido.ptr
is not a recognized format.
- ou --or-
ptr
é null
.ptr
is null
.
- ou --or-
ptr
é inválido.ptr
is invalid.
Exemplos
O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando os métodos ReadInt32 e WriteInt32.The following example demonstrates how to read and write to an unmanaged array using the ReadInt32 and WriteInt32 methods.
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
O exemplo a seguir demonstra como usar o método ReadInt32 para ler o valor de uma variável de int
não gerenciada.The following example demonstrates how to use the ReadInt32 method to read the value of an unmanaged int
variable.
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);
}
Comentários
ReadInt32 tem um deslocamento implícito de 0.ReadInt32 has an implied offset of 0. Esse método habilita a interação direta com uma matriz de Int32
de estilo C não gerenciado, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.This method enables direct interaction with an unmanaged C-style Int32
array, eliminating the expense of copying an entire unmanaged array (using Marshal.Copy) to a separate managed array before reading its element values.
Há suporte para leitura de locais de memória desalinhados.Reading from unaligned memory locations is supported.
Segurança
SecurityCriticalAttribute
requer confiança total para o chamador imediato.requires full trust for the immediate caller. Este membro não pode ser usado pelo código transparente ou parcialmente confiável.This member cannot be used by partially trusted or transparent code.
Veja também
ReadInt32(IntPtr, Int32)
Lê um inteiro com sinal de 32 bits em um deslocamento fornecido na memória não gerenciada.Reads a 32-bit signed integer at a given offset from unmanaged memory.
public:
static int ReadInt32(IntPtr ptr, int ofs);
[System.Security.SecurityCritical]
public static int ReadInt32 (IntPtr ptr, int ofs);
static member ReadInt32 : nativeint * int -> int
Public Shared Function ReadInt32 (ptr As IntPtr, ofs As Integer) As Integer
Parâmetros
- ptr
- IntPtr
O endereço básico na memória não gerenciada no qual a leitura será realizada.The base address in unmanaged memory from which to read.
- ofs
- Int32
Um deslocamento de byte adicional, adicionado ao parâmetro ptr
antes de ler.An additional byte offset, which is added to the ptr
parameter before reading.
Retornos
O inteiro com sinal de 32 bits da memória não gerenciada.The 32-bit signed integer read from unmanaged memory.
- Atributos
Exceções
O endereço básico (ptr
) e o deslocamento de byte (ofs
) produz um endereço nulo ou inválido.Base address (ptr
) plus offset byte (ofs
) produces a null or invalid address.
Exemplos
O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando os métodos ReadInt32 e WriteInt32.The following example demonstrates how to read and write to an unmanaged array using the ReadInt32 and WriteInt32 methods.
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
O exemplo a seguir demonstra como usar o método ReadInt32 para ler o valor de uma variável de int
não gerenciada.The following example demonstrates how to use the ReadInt32 method to read the value of an unmanaged int
variable.
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);
}
Comentários
ReadInt32 habilita a interação direta com uma matriz assinada de 32 bits não gerenciada, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.ReadInt32 enables direct interaction with an unmanaged 32-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.
Há suporte para leitura de locais de memória desalinhados.Reading from unaligned memory locations is supported.
Segurança
SecurityCriticalAttribute
requer confiança total para o chamador imediato.requires full trust for the immediate caller. Este membro não pode ser usado pelo código transparente ou parcialmente confiável.This member cannot be used by partially trusted or transparent code.
Veja também
ReadInt32(Object, Int32)
Aviso
Esta API agora é obsoleta.
Lê um inteiro com sinal de 32 bits em um deslocamento fornecido na memória não gerenciada.Reads a 32-bit signed integer at a given offset from unmanaged memory.
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);
static member ReadInt32 : obj * int -> int
Public Shared Function ReadInt32 (ptr As Object, ofs As Integer) As Integer
Parâmetros
- ptr
- Object
O endereço básico em memória não gerenciada do objeto de origem.The base address in unmanaged memory of the source object.
- ofs
- Int32
Um deslocamento de byte adicional, adicionado ao parâmetro ptr
antes de ler.An additional byte offset, which is added to the ptr
parameter before reading.
Retornos
O inteiro com sinal de 32 bits lido da memória não gerenciada no deslocamento especificado.The 32-bit signed integer read from unmanaged memory at the given offset.
- Atributos
Exceções
O endereço básico (ptr
) e o deslocamento de byte (ofs
) produz um endereço nulo ou inválido.Base address (ptr
) plus offset byte (ofs
) produces a null or invalid address.
ptr
é um objeto ArrayWithOffset.ptr
is an ArrayWithOffset object. Esse método não aceita parâmetros ArrayWithOffset.This method does not accept ArrayWithOffset parameters.
Comentários
ReadInt32 habilita a interação direta com uma matriz assinada de 32 bits não gerenciada, eliminando a despesa de copiar uma matriz não gerenciada inteira (usando Marshal.Copy) para uma matriz gerenciada separada antes de ler seus valores de elemento.ReadInt32 enables direct interaction with an unmanaged 32-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.
Há suporte para leitura de locais de memória desalinhados.Reading from unaligned memory locations is supported.
Segurança
SecurityCriticalAttribute
requer confiança total para o chamador imediato.requires full trust for the immediate caller. Este membro não pode ser usado pelo código transparente ou parcialmente confiável.This member cannot be used by partially trusted or transparent code.