UnmanagedMemoryStream コンストラクタ (Byte*, Int64, Int64, FileAccess)
アセンブリ: mscorlib (mscorlib.dll 内)

public: UnmanagedMemoryStream ( unsigned char* pointer, long long length, long long capacity, FileAccess access )
- access
FileAccess 値の 1 つ。


length パラメータは、使用する現在のメモリ量を定義します。ストリームからデータを読み取ったり、ストリームにデータを追加したりする場合は、length 値が、読み取ったり保持したりするストリーム内の有効なデータの量と等しい必要があります。ストリームに書き込む場合は、この値は 0 であることが必要です。
capacity パラメータは、使用できるメモリ総量を示します。この値は、指定した長さよりも長い領域を示すことも、追加できる領域を示すこともできます。この値を上回る量を書き込もうとすると失敗します。
access パラメータは、CanRead プロパティと CanWrite プロパティを設定します。Write を指定しても、ストリームが書き込み可能であるという保証はありません。access パラメータを使用すると、実装側はオブジェクトを作成し、その実装を公開される実際のストリームに一致させることができます。

UnmanagedMemoryStream クラスを使用して、アンマネージ メモリから読み取る方法と、アンマネージ メモリに書き込む方法を次のコード例に示します。アンマネージ メモリのブロックは、Marshal クラスを使用して割り当ておよび割り当て解除されます。
// 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 som 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(); } }

- UnmanagedCode (アンマネージ メモリにアクセスするために必要なアクセス許可)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


UnmanagedMemoryStream コンストラクタ

名前 | 説明 |
---|---|
UnmanagedMemoryStream () | UnmanagedMemoryStream クラスの新しい空のインスタンスを初期化します。 |
UnmanagedMemoryStream (Byte*, Int64) | 指定した位置とメモリ長を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。 |
UnmanagedMemoryStream (Byte*, Int64, Int64, FileAccess) | 指定した位置、メモリ長、メモリ総量、およびファイル アクセス値を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。 |

UnmanagedMemoryStream コンストラクタ (Byte*, Int64)
アセンブリ: mscorlib (mscorlib.dll 内)



このコンストラクタは、UnmanagedMemoryStream クラスの新しいインスタンスを作成します。また、既定では、CanWrite プロパティに false を設定し、CanRead プロパティに true を設定します。Length プロパティには length パラメータの値が設定され、変更することはできません。

UnmanagedMemoryStream クラスを使用して、アンマネージ メモリから読み取る方法と、アンマネージ メモリに書き込む方法を次のコード例に示します。アンマネージ メモリのブロックは、Marshal クラスを使用して割り当ておよび割り当て解除されます。
// 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)); } }

- UnmanagedCode (アンマネージ メモリにアクセスするために必要なアクセス許可)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


UnmanagedMemoryStream コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)



- UnmanagedCode (アンマネージ メモリにアクセスするために必要なアクセス許可)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- UnmanagedMemoryStream コンストラクタのページへのリンク