質問

class Foo
{
   static bool Bar(Stream^ stream);
};

class FooWrapper
{
   bool Bar(LPCWSTR szUnicodeString)
   {
       return Foo::Bar(??);
   }
};

MemoryStream かかります byte[] でも私はそうするだろう のように 可能であればデータをコピーせずにこれを実行します。

役に立ちましたか?

解決

を使用するとコピーを回避できます UnmanagedMemoryStream() 代わりに (クラスは .NET FCL 2.0 以降に存在します)。のように MemoryStream, のサブクラスです。 IO.Stream, 、通常のストリーム操作がすべて含まれています。

Microsoft によるこのクラスの説明は次のとおりです。

マネージ コードからアンマネージ メモリ ブロックへのアクセスを提供します。

これで、知っておくべきことがほぼわかります。ご了承ください UnmanagedMemoryStream() CLS に準拠していません。

他のヒント

メモリをコピーする必要がある場合は、次のようにすればうまくいくと思います。


static Stream^ UnicodeStringToStream(LPCWSTR szUnicodeString)
{
   //validate the input parameter
   if (szUnicodeString == NULL)
   {
      return nullptr;
   }

   //get the length of the string
   size_t lengthInWChars = wcslen(szUnicodeString);  
   size_t lengthInBytes = lengthInWChars * sizeof(wchar_t);

   //allocate the .Net byte array
   array^ byteArray = gcnew array(lengthInBytes);

   //copy the unmanaged memory into the byte array
   Marshal::Copy((IntPtr)(void*)szUnicodeString, byteArray, 0, lengthInBytes);

   //create a memory stream from the byte array
   return gcnew MemoryStream(byteArray);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top