Método Marshal.WriteInt16 (IntPtr, Char)
Publicado: octubre de 2016
Escribe un carácter como un valor entero de 16 bits en la memoria no administrada.
Espacio de nombres: System.Runtime.InteropServices
Ensamblado: mscorlib (en mscorlib.dll)
Sintaxis
[SecurityCriticalAttribute]
public static void WriteInt16(
IntPtr ptr,
char val
)
public:
[SecurityCriticalAttribute]
static void WriteInt16(
IntPtr ptr,
wchar_t val
)
[<SecurityCriticalAttribute>]
static member WriteInt16 :
ptr:nativeint *
val:char -> unit
<SecurityCriticalAttribute>
Public Shared Sub WriteInt16 (
ptr As IntPtr,
val As Char
)
Parámetros
ptr
Type: System.IntPtrDirección de la memoria no administrada en la que se va a escribir.
val
Type: System.CharValor que se va a escribir.
Excepciones
Exception | Condition |
---|---|
AccessViolationException | ptr no es un formato reconocido. O bien El valor de ptr es null. O bien ptr no es válido. |
Comentarios
WriteInt16 permite una interacción directa con una matriz de 16 bits con signo no administrada, elimina el gasto de copiar toda una matriz no administrada (mediante Marshal.Copy) a una matriz administrada distinta antes de establecer los valores de sus elementos.
Se admite la escritura en ubicaciones de memoria desalineadas.
Ejemplos
En el ejemplo siguiente se muestra cómo leer y escribir en una matriz no administrada mediante el ReadInt16 y WriteInt16 métodos.
static void ReadWriteInt16()
{
// Allocate unmanaged memory.
int elementSize = 2;
IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);
// Set the 10 elements of the C-style unmanagedArray
for (int i = 0; i < 10; i++)
{
Marshal.WriteInt16(unmanagedArray, i * elementSize, ((Int16)(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.ReadInt16(unmanagedArray, i * elementSize));
}
Marshal.FreeHGlobal(unmanagedArray);
Console.WriteLine("Done. Press Enter to continue.");
Console.ReadLine();
}
Sub ReadWriteInt16()
' Allocate unmanaged memory.
Dim elementSize As Integer = 2
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.WriteInt16(unmanagedArray, i * elementSize, CType(i + 1, Int16))
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.ReadInt16(unmanagedArray, i * elementSize))
Next i
Marshal.FreeHGlobal(unmanagedArray)
Console.WriteLine("Done. Press Enter to continue.")
Console.ReadLine()
End Sub
Seguridad
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Información de versión
Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 1.1
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Windows Phone
Disponible desde 8.1
Ver también
Copy
WriteInt16 Sobrecarga
Clase Marshal
Espacio de nombres System.Runtime.InteropServices
Volver al principio