class Foo
{
static bool Bar(Stream^ stream);
};
class FooWrapper
{
bool Bar(LPCWSTR szUnicodeString)
{
return Foo::Bar(??);
}
};
MemoryStream
時間がかかりますが、可能であればデータをコピーせずにこれを実行しbyte[]
たいと思います。
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
、すべての通常のストリーム操作を備えています。
クラスのマイクロソフトの説明は次のとおりです。
マネージ コードからアンマネージ メモリ ブロックへのアクセスを提供します。
これは、あなたが知る必要があることをほとんど教えてくれます。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);
}