Writing std::iostream to MemoryStream

G

Guest

Hi,

I have an unmanaged iostream type that I need to copy to MemoryStream
(managed). Will appreciate if somebody can provide me the best/fastest
approach. This is what I have.

std::iostream *pStream = getStream();

//I need to copy the data pointed by pStream to MemoryStream
System::IO::MemoryStream ^mStream = gcnew System::IO::MemoryStream()

Thanks for the help.
 
T

Tamas Demjen

Haxan said:
I have an unmanaged iostream type that I need to copy to MemoryStream
(managed). Will appreciate if somebody can provide me the best/fastest
approach.

Probably you could call SetLength to set the MemoryStream's size, and
then pin its buffer and use iostream to read directly into MemoryStream.
Something like this should work:

System::IO::MemoryStream ^ mStream = gcnew System::IO::MemoryStream;
mStream->SetLength(bytes_to_read);
pin_ptr<unsigned char> pp(&mStream->GetBuffer()[0]);
std::iostream * pStream = getStream();
pStream->read(reinterpret_cast<char*>(pp), bytes_to_read);

I've done the opposite before: I was writing to MemoryStream from
managed code, pinned it, and read its content from native code, and it
worked fine.

Tom
 
G

Guest

Thanks Tamas, that is really helpful.

Tamas Demjen said:
Haxan said:
I have an unmanaged iostream type that I need to copy to MemoryStream
(managed). Will appreciate if somebody can provide me the best/fastest
approach.

Probably you could call SetLength to set the MemoryStream's size, and
then pin its buffer and use iostream to read directly into MemoryStream.
Something like this should work:

System::IO::MemoryStream ^ mStream = gcnew System::IO::MemoryStream;
mStream->SetLength(bytes_to_read);
pin_ptr<unsigned char> pp(&mStream->GetBuffer()[0]);
std::iostream * pStream = getStream();
pStream->read(reinterpret_cast<char*>(pp), bytes_to_read);

I've done the opposite before: I was writing to MemoryStream from
managed code, pinned it, and read its content from native code, and it
worked fine.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top