Marshal.WriteByte-Methode: (IntPtr, Byte)
Veröffentlicht: Oktober 2016
Schreibt einen einzelnen Bytewert in den nicht verwalteten Speicher.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Syntax
[SecurityCriticalAttribute]
public static void WriteByte(
IntPtr ptr,
byte val
)
public:
[SecurityCriticalAttribute]
static void WriteByte(
IntPtr ptr,
unsigned char val
)
[<SecurityCriticalAttribute>]
static member WriteByte :
ptr:nativeint *
val:byte -> unit
<SecurityCriticalAttribute>
Public Shared Sub WriteByte (
ptr As IntPtr,
val As Byte
)
Parameter
ptr
Type: System.IntPtrDie Adresse im nicht verwalteten Speicher, in die geschrieben werden soll.
val
Type: System.ByteDer zu schreibende Wert.
Ausnahmen
Exception | Condition |
---|---|
AccessViolationException | ptr ist kein bekanntes Format. - oder - ptr ist null. - oder - ptr ist ungültig. |
Hinweise
WriteByte ermöglicht die direkte Interaktion mit einem nicht verwalteten C-Stil-Bytearray, sodass ein nicht verwaltetes Array kopiert (mit Marshal.Copy) in ein separates verwaltetes Array vor Elementwerte.
Beispiele
Das folgende Beispiel erstellt einen nicht verwalteten Speicherblock, schreibt ein Byte in den nicht verwalteten Speicher, liest Bytes aus dem nicht verwalteten Speicher und dann verwirft den nicht verwalteten Speicher.
using System;
using System.Runtime.InteropServices;
class Example
{
static void Main(string[] args)
{
// Allocate 1 byte of unmanaged memory.
IntPtr hGlobal = Marshal.AllocHGlobal(1);
// Create a new byte.
byte b = 1;
Console.WriteLine("Byte written to unmanaged memory: " + b);
// Write the byte to unmanaged memory.
Marshal.WriteByte(hGlobal, b);
// Read byte from unmanaged memory.
byte c = Marshal.ReadByte(hGlobal);
Console.WriteLine("Byte read from unmanaged memory: " + c);
// Free the unmanaged memory.
Marshal.FreeHGlobal(hGlobal);
Console.WriteLine("Unmanaged memory was disposed.");
}
}
Imports System
Imports System.Runtime.InteropServices
Module Example
Sub Main()
' Allocate 1 byte of unmanaged memory.
Dim hGlobal As IntPtr = Marshal.AllocHGlobal(1)
' Create a new byte.
Dim b As Byte = 1
Console.WriteLine("Byte written to unmanaged memory: {0}", b)
' Write the byte to unmanaged memory.
Marshal.WriteByte(hGlobal, b)
' Read byte from unmanaged memory.
Dim c As Byte = Marshal.ReadByte(hGlobal)
Console.WriteLine("Byte read from unmanaged memory: {0}", c)
' Free the unmanaged memory.
Marshal.FreeHGlobal(hGlobal)
Console.WriteLine("Unmanaged memory was disposed.")
End Sub
End Module
Sicherheit
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Versionsinformationen
Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1
Siehe auch
Copy
WriteByte Überladen
Marshal-Klasse
System.Runtime.InteropServices-Namespace
Zurück zum Anfang