I have a DLL streaming text to std::cerr
. The EXE linking (via LoadLibrary()
) to that DLL needs to get the text being streamed.
I am using VS90.
I use the following class in the EXE for the redirection:
struct cerr_redirect {
cerr_redirect( std::streambuf * new_buffer )
: old( std::cerr.rdbuf( new_buffer ) )
{ }
~cerr_redirect( ) {
std::cerr.rdbuf( old );
}
private:
std::streambuf * old;
};
This works fine as long as both DLL and EXE are build using the same configuration (either both Debug or both Release). However, when running a Release built EXE with a Debug built DLL, the redirection does not work and none of the cerr
output made in the DLL gets to the EXE.
EDIT
The Runtime Library is linked dynamically. The Debug built DLL links against "Multi-threaded Debug DLL (/MDd)" while the Release built EXE links against "Multi-threaded DLL (/MD)".
I suspect this is the reason for this behavior; I believe std::cerr
lives in the CRT. So when both DLL and EXE use the same CRT they share the same std::cerr
, otherwise, they each have their own copy.
Could someone confirm if my understanding is correct?
If so, I think a solution to my problem is to define my own global stream in the EXE and let the DLL redirect cerr
to it. Or is there a more elegant solution?