Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an IntPtr to a Stream?

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

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

MemoryStream will take a byte[] but I'd like to do this without copying the data if possible.

like image 808
Adam Tegen Avatar asked Nov 17 '25 22:11

Adam Tegen


1 Answers

You can avoid the copy if you use an UnmanagedMemoryStream() instead (class exists in .NET FCL 2.0 and later). Like MemoryStream, it is a subclass of IO.Stream, and has all the usual stream operations.

Microsoft's description of the class is:

Provides access to unmanaged blocks of memory from managed code.

which pretty much tells you what you need to know. Note that UnmanagedMemoryStream() is not CLS-compliant.

like image 165
McKenzieG1 Avatar answered Nov 20 '25 10:11

McKenzieG1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!