<pre><code>class Foo
{
   static bool Bar(Stream^ stream);
};

class FooWrapper
{
   bool Bar(LPCWSTR szUnicodeString)
   {
       return Foo::Bar(??);
   }
};
</code></pre>

<p><code>MemoryStream</code> will take a <code>byte[]</code> but I'd <em>like</em> to do this without copying the data if possible.</p>

[{'answer_id': 42605, 'author': 'Adam Tegen', 'author_id': 4066, 'author_profile': 'https://Stackoverflow.com/users/4066', 'pm_score': 0, 'selected': False, 'text': '<p>If I had to copy the memory, I think the following would work:</p>\n\n<pre><code>\nstatic Stream^ UnicodeStringToStream(LPCWSTR szUnicodeString)\n{\n   //validate the input parameter\n   if (szUnicodeString == NULL)\n   {\n      return nullptr;\n   }\n\n   //get the length of the string\n   size_t lengthInWChars = wcslen(szUnicodeString);  \n   size_t lengthInBytes = lengthInWChars * sizeof(wchar_t);\n\n   //allocate the .Net byte array\n   array^ byteArray = gcnew array(lengthInBytes);\n\n   //copy the unmanaged memory into the byte array\n   Marshal::Copy((IntPtr)(void*)szUnicodeString, byteArray, 0, lengthInBytes);\n\n   //create a memory stream from the byte array\n   return gcnew MemoryStream(byteArray);\n}</code></pre>\n'}, {'answer_id': 42968, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 4, 'selected': True, 'text': '<p>You can avoid the copy if you use an <a href="http://msdn.microsoft.com/en-us/library/system.io.unmanagedmemorystream.aspx" rel="nofollow noreferrer"><code>UnmanagedMemoryStream()</code></a> instead (class exists in .NET FCL 2.0 and later). Like <code>MemoryStream</code>, it is a subclass of <code>IO.Stream</code>, and has all the usual stream operations.</p>\n\n<p>Microsoft\'s description of the class is:</p>\n\n<blockquote>\n  <p>Provides access to unmanaged blocks of memory from managed code.</p>\n</blockquote>\n\n<p>which pretty much tells you what you need to know. Note that <code>UnmanagedMemoryStream()</code> is not CLS-compliant.</p>\n'}]
['https://Stackoverflow.com/questions/42446', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4066/']