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



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



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 コンストラクタ (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 プロパティ

名前 | 説明 | |
---|---|---|
![]() | CanRead | オーバーライドされます。 ストリームが読み取りをサポートしているかどうかを示す値を取得します。 |
![]() | CanSeek | オーバーライドされます。 ストリームがシークをサポートしているかどうかを示す値を取得します。 |
![]() | CanTimeout | 現在のストリームがタイムアウトできるかどうかを決定する値を取得します。 ( Stream から継承されます。) |
![]() | CanWrite | オーバーライドされます。 ストリームが書き込みをサポートしているかどうかを示す値を取得します。 |
![]() | Capacity | ストリームの長さ (サイズ)、またはストリームに割り当てられたメモリの総量 (容量) を取得します。 |
![]() | Length | オーバーライドされます。 ストリーム内のデータ長を取得します。 |
![]() | Position | オーバーライドされます。 ストリーム内の現在位置を取得または設定します。 |
![]() | PositionPointer | ストリーム内の現在位置に基づいて、ストリームへのバイト ポインタを取得または設定します。 |
![]() | ReadTimeout | ストリームがタイムアウト前に読み取りを試行する期間を決定する値を取得または設定します。 ( Stream から継承されます。) |
![]() | WriteTimeout | ストリームがタイムアウト前に書き込みを試行する期間を決定する値を取得または設定します。 ( Stream から継承されます。) |

UnmanagedMemoryStream メソッド


名前 | 説明 | |
---|---|---|
![]() | CreateWaitHandle | WaitHandle オブジェクトを割り当てます。 ( Stream から継承されます。) |
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | Initialize | UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

UnmanagedMemoryStream メンバ
マネージ コードからメモリのアンマネージ ブロックにアクセスできるようにします。
UnmanagedMemoryStream データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | UnmanagedMemoryStream | オーバーロードされます。 UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。 |


名前 | 説明 | |
---|---|---|
![]() | CanRead | オーバーライドされます。 ストリームが読み取りをサポートしているかどうかを示す値を取得します。 |
![]() | CanSeek | オーバーライドされます。 ストリームがシークをサポートしているかどうかを示す値を取得します。 |
![]() | CanTimeout | 現在のストリームがタイムアウトできるかどうかを決定する値を取得します。(Stream から継承されます。) |
![]() | CanWrite | オーバーライドされます。 ストリームが書き込みをサポートしているかどうかを示す値を取得します。 |
![]() | Capacity | ストリームの長さ (サイズ)、またはストリームに割り当てられたメモリの総量 (容量) を取得します。 |
![]() | Length | オーバーライドされます。 ストリーム内のデータ長を取得します。 |
![]() | Position | オーバーライドされます。 ストリーム内の現在位置を取得または設定します。 |
![]() | PositionPointer | ストリーム内の現在位置に基づいて、ストリームへのバイト ポインタを取得または設定します。 |
![]() | ReadTimeout | ストリームがタイムアウト前に読み取りを試行する期間を決定する値を取得または設定します。 (Stream から継承されます。) |
![]() | WriteTimeout | ストリームがタイムアウト前に書き込みを試行する期間を決定する値を取得または設定します。 (Stream から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | CreateWaitHandle | WaitHandle オブジェクトを割り当てます。 (Stream から継承されます。) |
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | Initialize | UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

- UnmanagedMemoryStreamのページへのリンク