Compartilhar via


Método Marshal.WriteIntPtr (IntPtr, 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.

Grava um valor de inteiro de tamanho nativo de processador na memória não gerenciada.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (em mscorlib.dll)

Sintaxe

[SecurityCriticalAttribute]
public static void WriteIntPtr(
    IntPtr ptr,
    IntPtr val
)
public:
[SecurityCriticalAttribute]
static void WriteIntPtr(
    IntPtr ptr,
    IntPtr val
)
[<SecurityCriticalAttribute>]
static member WriteIntPtr : 
        ptr:nativeint *
        val:nativeint -> unit
<SecurityCriticalAttribute>
Public Shared Sub WriteIntPtr (
    ptr As IntPtr,
    val As IntPtr
)

Parâmetros

  • ptr
    Type: System.IntPtr

    O endereço a ser gravado na 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

WriteIntPtrHabilita interação direta com um estilo C não gerenciado IntPtr matriz, eliminando a despesa de cópia de uma matriz não gerenciada toda (usando Marshal.Copy) em uma matriz separada gerenciada antes de definir seus valores de elemento.

Há suporte para gravar em locais de memória não alinhados.

Exemplos

O exemplo a seguir demonstra como ler e gravar em uma matriz não gerenciada usando o ReadIntPtr e WriteIntPtr métodos.

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

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

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteIntPtr()
    ' Allocate unmanaged memory.
    Dim elementSize As Integer = Marshal.SizeOf(GetType(IntPtr))
    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.WriteIntPtr(unmanagedArray, i * elementSize, CType(i + 1, IntPtr))
    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.ReadIntPtr(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

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

Segurança

SecurityCriticalAttribute

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
WriteIntPtr Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices

Retornar ao início