Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

UnmanagedMemoryStream Constructor (Byte*, Int64, Int64, FileAccess)

Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

This API is not CLS-compliant. 

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
[CLSCompliantAttribute(false)]
public UnmanagedMemoryStream(
	byte* pointer,
	long length,
	long capacity,
	FileAccess access
)

Parameters

pointer
Type: System.Byte*

A pointer to an unmanaged memory location.

length
Type: System.Int64

The length of the memory to use.

capacity
Type: System.Int64

The total amount of memory assigned to the stream.

access
Type: System.IO.FileAccess

One of the FileAccess values.

ExceptionCondition
SecurityException

The user does not have the required permission.

ArgumentNullException

The pointer value is null.

ArgumentOutOfRangeException

The length value is less than zero.

- or -

The capacity value is less than zero.

- or -

The length value is greater than the capacity value.

The length parameter defines the current amount of memory in use. If reading or appending data to the stream, the length value should be equal to the amount of valid data in the stream to be read from or preserved. If writing to the stream, this value should be zero.

The capacity parameter indicates the amount of total memory available. This value can describe a region that is longer than the length specified, or indicate a region that can be appended to. Any attempt to write beyond this value will fail.

The access parameter sets the CanRead, and CanWrite properties. Note that specifying Write does not guarantee that the stream will be writable. The access parameters allow the implementer to create an object whose implementation can match the actual stream that is exposed.

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();
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.