Redirect stderr of an imported C DLL to an arbitrary file
<p>Python version = 2.7, Windows</p>

<p>Hi,</p>

<p>My Python script uses a shared C library and I want to redirect the standard IOs of the script and the shared C library in a file as in the following example (limited to the stderr stream)</p>

<p>C shared library code:</p>

<pre><code>#include &lt;stdio.h&gt;

void my_print(void)
{
    fprintf(stderr, "This string isn't displayed\n");
}
</code></pre>

<p>The Python script:</p>

<pre><code>import os, sys, ctypes

def test():
    parserDll = ctypes.CDLL("./titi.dll")           # my shared C library
    file_stderr_fds = os.open("./toto.txt", 777)    # file in which redirect the stderr
    saved_stderr_fds = os.dup(2)                    # Backup stderr
    os.dup2(file_stderr_fds, 2)                     # Redirect stderr to file
    print &gt;&gt; sys.stderr, "zozo"                     # Python test redirect for Python script --&gt; Ok
    parserDll.my_print(None)                        # test for the shared C library --&gt; KO

if __name__ == "__main__":
    test()
</code></pre>

<p>When I run this Python script, in the toto.txt file I read "zozo" (displayed from the Python script) but nothing from the shared C library. However, if I comment os.dup2() out, both strings are displayed in the console.
Why?
Aren't standard IOs the same for the Python script and the shared C library that is used? In this case, how can I redirect both to the same file?</p>
