Método Marshal.ReadInt32 (IntPtr)
Dica
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.
Lê um inteiro com sinal de 32 bits da memória não gerenciada.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SecurityCriticalAttribute]
public static int ReadInt32(
IntPtr ptr
)
public:
[SecurityCriticalAttribute]
static int ReadInt32(
IntPtr ptr
)
[<SecurityCriticalAttribute>]
static member ReadInt32 :
ptr:nativeint -> int
<SecurityCriticalAttribute>
Public Shared Function ReadInt32 (
ptr As IntPtr
) As Integer
Parâmetros
ptr
Type: System.IntPtrO endereço na memória não gerenciada do qual ler.
Valor Retornado
Type: System.Int32
O inteiro com sinal de 32 bits da memória não gerenciada.
Exceções
Exception | Condition |
---|---|
AccessViolationException | ptr não é um formato reconhecido. -ou- ptr é null. -ou- ptr é inválido. |
Comentários
ReadInt32tem um deslocamento implícito de 0. Esse método permite interação direta com um estilo C não gerenciado Int32 matriz, eliminando a despesa de cópia de uma matriz não gerenciada toda (usando Marshal.Copy) em uma matriz separada gerenciada antes de ler os valores de elemento.
Há suporte para leitura de locais de memória desalinhados.
Exemplos
O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando o ReadInt32 e WriteInt32 métodos.
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 ReadInt32 método para ler o valor de um não gerenciado int variável.
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);
}
Segurança
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Silverlight
Disponível desde 2.0
Windows Phone Silverlight
Disponível desde 7.0
Windows Phone
Disponível desde 8.1
Confira Também
Copy
ReadInt32 Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices
Retornar ao início