Redirect Standard Out, The old (C) style
We have a third party library that outputs some debug/trace information when executes, this is of course not very nice when you have a command line application that calls the library.
The solution we used is to have a wrapper around the library, which redirects stdout before the call and restores it after.
An initial attempt to do this with Windows APIs (GetStdHandle/SetStdHandle) was unsuccessful probably because C runtime keeps its own handles. So, we had to go down and dirty to the low level C APIs (and this works fine):
//keep a handle and close stdout
int handle = _dup(1);
_close(1);
// call the lib function here
//restore stdout, close the copy
_dup2(handle, 1);
_close(handle);
