If I understand correctly, you want to wrap a .NET stream with a std C ++ stream so that your own code is passed to the std C ++ stream, but the data goes into the .NET stream.
C ++ IO streams are roughly divided into the streams themselves, which perform all the conversions between the C ++ types and the binary representation, and the stream buffers that buffer data and read from / write to the device. What you need to do to achieve your goal is to use a stream buffer that writes to the .NET stream. To do this, you need to create your own stream buffer, obtained from std::stream_buffer , which internally refers to the .NET stream and sends all the data to it. This is what you pass to the std::ostream , which is passed into native code.
Writing your own stream buffer is not a novice task, but it is not particularly difficult. Choose any worthy link to IO C ++ streams ( Langer / Kreft is the best you can get on paper), find out which of the virtual functions you need to rewrite to do this, and you're done.
source share