[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Provides access to unmanaged blocks of memory from managed code.
Namespace:
System.IO
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Class UnmanagedMemoryStream _
Inherits Stream
Dim instance As UnmanagedMemoryStream
public class UnmanagedMemoryStream : Stream
public ref class UnmanagedMemoryStream : public Stream
type UnmanagedMemoryStream =
class
inherit Stream
end
This class supports access to unmanaged memory using the existing stream-based model and does not require that the contents in the unmanaged memory be copied to the heap.
The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. A block of unmanaged memory is allocated and de-allocated using the Marshal class.
// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
unsafe class TestWriter
{
static void Main()
{
// Create some data to read and write.
byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");
// Allocate a block of unmanaged memory and return an IntPtr object.
IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);
// Get a byte pointer from the IntPtr object.
byte* memBytePtr = (byte*) memIntPtr.ToPointer();
// Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);
// Write the data.
writeStream.Write(message, 0, message.Length);
// Close the stream.
writeStream.Close();
// Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);
// Create a byte array to hold data from unmanaged memory.
byte[] outMessage = new byte[message.Length];
// Read from unmanaged memory to the byte array.
readStream.Read(outMessage, 0, message.Length);
// Close the stream.
readStream.Close();
// Display the data to the console.
Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));
// Free the block of unmanaged memory.
Marshal.FreeHGlobal(memIntPtr);
Console.ReadLine();
}
}
System..::.Object
System..::.MarshalByRefObject
System.IO..::.Stream
System.IO..::.UnmanagedMemoryStream
System.IO.MemoryMappedFiles..::.MemoryMappedViewStream
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0
.NET Framework Client Profile
Supported in: 4
Reference