UnmanagedMemoryStream コンストラクタとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > UnmanagedMemoryStream コンストラクタの意味・解説 

UnmanagedMemoryStream コンストラクタ (Byte*, Int64, Int64, FileAccess)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

指定した位置メモリ長、メモリ総量、およびファイル アクセス値を使用して、UnmanagedMemoryStream クラス新しインスタンス初期化します。

このコンストラクタは、CLS準拠していません。  

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

SecurityException

ユーザー必要なアクセス許可がありません。

ArgumentNullException

pointer 値が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

length 値が 0 未満です。

または

capacity 値が 0 未満です。

または

length 値が capacity 値を超えてます。

解説解説
使用例使用例

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();
    }
}
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間

UnmanagedMemoryStream コンストラクタ

UnmanagedMemoryStream クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

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

関連項目

UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間

UnmanagedMemoryStream コンストラクタ (Byte*, Int64)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

指定した位置メモリ長を使用して、UnmanagedMemoryStream クラス新しインスタンス初期化します。

このコンストラクタは、CLS準拠していません。  

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

SecurityException

ユーザー必要なアクセス許可がありません。

ArgumentNullException

pointer 値が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

length 値が 0 未満です。

または

lengthオーバーフロー原因となりうる長さです。

解説解説

このコンストラクタは、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));
    }
}
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間

UnmanagedMemoryStream コンストラクタ ()

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

UnmanagedMemoryStream クラス新しい空のインスタンス初期化します。

このコンストラクタは、CLS準拠していません。  

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Dim instance As New UnmanagedMemoryStream
protected UnmanagedMemoryStream ()
protected:
UnmanagedMemoryStream ()
protected UnmanagedMemoryStream ()
protected function UnmanagedMemoryStream ()
例外例外
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「UnmanagedMemoryStream コンストラクタ」の関連用語

UnmanagedMemoryStream コンストラクタのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UnmanagedMemoryStream コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS