If I understood correctly, you want to wrap a .NET stream with a C++ std stream, so that your native code streams into the C++ std stream, but the data ends up in the .NET stream.
C++ IO streams roughly split into the streams themselves, which do all of the conversion between the C++ types and a binary representation, and the stream buffers, which buffer the data and read from/write to a device. What you would need to do in order to achieve you goal is to use a stream buffer that writes to a .NET stream. In order to do this, you need to create your own stream buffer, derived from std::stream_buffer
, which internally references a .NET stream and forwards all data to it. This you pass to the std::ostream
object which is passed to the native code.
Writing your own stream buffer isn't a beginner's task, but it isn't particularly hard either. Pick any decent reference on C++ IO streams (Langer/Kreft is the best you can get on paper), find out which of the virtual functions you need to overwrite in order to do that, and you're done.