UnmanagedMemoryStream.Write メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As UnmanagedMemoryStream Dim buffer As Byte() Dim offset As Integer Dim count As Integer instance.Write(buffer, offset, count)



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.Write メソッドのページへのリンク