UnmanagedMemoryStream UnmanagedMemoryStream UnmanagedMemoryStream UnmanagedMemoryStream Constructors

定義

多載

UnmanagedMemoryStream() UnmanagedMemoryStream() UnmanagedMemoryStream()

初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class.

UnmanagedMemoryStream(Byte*, Int64) UnmanagedMemoryStream(Byte*, Int64) UnmanagedMemoryStream(Byte*, Int64)

使用指定的位置和記憶體長度,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class using the specified location and memory length.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

在安全緩衝區中使用指定的位移和長度,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset and length.

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

使用指定的位置、記憶體長度、記憶體總數和檔案存取值,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

在安全緩衝區中使用指定的位移、長度和檔案存取,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

UnmanagedMemoryStream() UnmanagedMemoryStream() UnmanagedMemoryStream()

初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class.

protected:
 UnmanagedMemoryStream();
protected UnmanagedMemoryStream ();
Protected Sub New ()

例外狀況

使用者沒有所需要的使用權限。The user does not have the required permission.

安全性

UnmanagedCode
用於存取非受控記憶體。for accessing unmanaged memory.

UnmanagedMemoryStream(Byte*, Int64) UnmanagedMemoryStream(Byte*, Int64) UnmanagedMemoryStream(Byte*, Int64)

重要

此 API 不符合 CLS 規範。

使用指定的位置和記憶體長度,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class using the specified location and memory length.

public:
 UnmanagedMemoryStream(System::Byte* pointer, long length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public UnmanagedMemoryStream (byte* pointer, long length);
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 -> System.IO.UnmanagedMemoryStream

參數

pointer
Byte*

Unmanaged 記憶體位置的指標。A pointer to an unmanaged memory location.

length
Int64 Int64 Int64 Int64

要使用的記憶體長度。The length of the memory to use.

例外狀況

使用者沒有所需要的使用權限。The user does not have the required permission.

length 值小於 0。The length value is less than zero.

-或--or- length 的大小足以造成溢位。The length is large enough to cause an overflow.

範例

下列程式碼範例示範如何使用UnmanagedMemoryStream類別來讀取和寫入非受控記憶體。The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. 使用Marshal類別配置和解除配置非受控記憶體的區塊。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.Runtime.InteropServices;
using System.Text;

unsafe class Program
{
    static void Main()
    {
        // Create some data to write.
        byte[] text = UnicodeEncoding.Unicode.GetBytes("Data to write.");

        // Allocate a block of unmanaged memory.
        IntPtr memIntPtr = Marshal.AllocHGlobal(text.Length);

        // Get a byte pointer from the unmanaged memory block.
        byte* memBytePtr = (byte*)memIntPtr.ToPointer();

        UnmanagedMemoryStream writeStream =
            new UnmanagedMemoryStream(
            memBytePtr, text.Length, text.Length, FileAccess.Write);

        // Write the data.
        WriteToStream(writeStream, text);

        // Close the stream.
        writeStream.Close();

        // Create another UnmanagedMemoryStream for reading.
        UnmanagedMemoryStream readStream =
            new UnmanagedMemoryStream(memBytePtr, text.Length);

        // Display the contents of the stream to the console.
        PrintStream(readStream);

        // Close the reading stream.
        readStream.Close();

        // Free up the unmanaged memory.
        Marshal.FreeHGlobal(memIntPtr);

    }

    public static void WriteToStream(UnmanagedMemoryStream writeStream, byte[] text)
    {
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (writeStream.CanWrite)
        {
            // Write the data, byte by byte
            for (int i = 0; i < writeStream.Length; i++)
            {
                writeStream.WriteByte(text[i]);
            }
        }
    }

    public static void PrintStream(UnmanagedMemoryStream readStream)
    {
        byte[] text = new byte[readStream.Length];
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (readStream.CanRead)
        {
            // Write the data, byte by byte
            for (int i = 0; i < readStream.Length; i++)
            {
                text[i] = (byte)readStream.ReadByte();
            }
        }

        Console.WriteLine(UnicodeEncoding.Unicode.GetString(text));
    }
}

備註

UnmanagedMemoryStream函式會建立類別的新實例, 且預設會CanWrite將屬性設為false , 並CanRead將屬性true設定為。This constructor creates a new instance of the UnmanagedMemoryStream class, and by default sets the CanWrite property to false and the CanRead property to true. 屬性會設定為length參數的值, 而且無法變更。 LengthThe Length property is set to the value of the length parameter and cannot be changed.

安全性

UnmanagedCode
用於存取非受控記憶體。for accessing unmanaged memory.

SecurityCriticalAttribute
需要完全信任立即呼叫者。requires full trust for the immediate caller. 部分信任或透明的程式碼無法使用這個成員。This member cannot be used by partially trusted or transparent code.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64) UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

在安全緩衝區中使用指定的位移和長度,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset and length.

public:
 UnmanagedMemoryStream(System::Runtime::InteropServices::SafeBuffer ^ buffer, long offset, long length);
public UnmanagedMemoryStream (System.Runtime.InteropServices.SafeBuffer buffer, long offset, long length);
new System.IO.UnmanagedMemoryStream : System.Runtime.InteropServices.SafeBuffer * int64 * int64 -> System.IO.UnmanagedMemoryStream
Public Sub New (buffer As SafeBuffer, offset As Long, length As Long)

參數

buffer
SafeBuffer SafeBuffer SafeBuffer SafeBuffer

包含 Unmanaged 記憶體資料流的緩衝區。The buffer to contain the unmanaged memory stream.

offset
Int64 Int64 Int64 Int64

緩衝區中的位元組位置,Unmanaged 記憶體資料流會在此處開始。The byte position in the buffer at which to start the unmanaged memory stream.

length
Int64 Int64 Int64 Int64

Unmanaged 記憶體資料流的長度。The length of the unmanaged memory stream.

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

重要

此 API 不符合 CLS 規範。

使用指定的位置、記憶體長度、記憶體總數和檔案存取值,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

public:
 UnmanagedMemoryStream(System::Byte* pointer, long length, long capacity, System::IO::FileAccess access);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public UnmanagedMemoryStream (byte* pointer, long length, long capacity, System.IO.FileAccess access);
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream

參數

pointer
Byte*

Unmanaged 記憶體位置的指標。A pointer to an unmanaged memory location.

length
Int64 Int64 Int64 Int64

要使用的記憶體長度。The length of the memory to use.

capacity
Int64 Int64 Int64 Int64

指派給資料流的記憶體總量。The total amount of memory assigned to the stream.

access
FileAccess FileAccess FileAccess FileAccess

其中一個 FileAccess 值。One of the FileAccess values.

例外狀況

使用者沒有所需要的使用權限。The user does not have the required permission.

length 值小於 0。The length value is less than zero.

-或--or- capacity 值小於 0。The capacity value is less than zero.

-或--or- length 值大於 capacity 值。The length value is greater than the capacity value.

範例

下列程式碼範例示範如何使用UnmanagedMemoryStream類別來讀取和寫入非受控記憶體。The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. 使用Marshal類別配置和解除配置非受控記憶體的區塊。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();
    }
}

備註

length參數會定義目前使用中的記憶體數量。The length parameter defines the current amount of memory in use. 如果將資料讀取或附加至資料流程, 此length值應該等於要讀取或保留之資料流程中的有效資料量。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.

capacity參數表示可用的總記憶體量。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.

參數會設定、和CanWrite屬性。 CanRead accessThe access parameter sets the CanRead, and CanWrite properties. 請注意, Write指定並不保證資料流程將可寫入。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.

安全性

UnmanagedCode
用於存取非受控記憶體。for accessing unmanaged memory.

SecurityCriticalAttribute
需要完全信任立即呼叫者。requires full trust for the immediate caller. 部分信任或透明的程式碼無法使用這個成員。This member cannot be used by partially trusted or transparent code.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess) UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

在安全緩衝區中使用指定的位移、長度和檔案存取,初始化 UnmanagedMemoryStream 類別的新執行個體。Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

public:
 UnmanagedMemoryStream(System::Runtime::InteropServices::SafeBuffer ^ buffer, long offset, long length, System::IO::FileAccess access);
public UnmanagedMemoryStream (System.Runtime.InteropServices.SafeBuffer buffer, long offset, long length, System.IO.FileAccess access);
new System.IO.UnmanagedMemoryStream : System.Runtime.InteropServices.SafeBuffer * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream
Public Sub New (buffer As SafeBuffer, offset As Long, length As Long, access As FileAccess)

參數

buffer
SafeBuffer SafeBuffer SafeBuffer SafeBuffer

包含 Unmanaged 記憶體資料流的緩衝區。The buffer to contain the unmanaged memory stream.

offset
Int64 Int64 Int64 Int64

緩衝區中的位元組位置,Unmanaged 記憶體資料流會在此處開始。The byte position in the buffer at which to start the unmanaged memory stream.

length
Int64 Int64 Int64 Int64

Unmanaged 記憶體資料流的長度。The length of the unmanaged memory stream.

access
FileAccess FileAccess FileAccess FileAccess

Unmanaged 記憶體資料流的檔案存取模式。The mode of file access to the unmanaged memory stream.

適用於