title: AttachConsole in DllMain: console appears and disappears immediately again
body: <p>I am trying to attach a console to a running c++ (console-less) program under Windows. As I have no access to the program's source code, only to the interface exposed for the purposes of writing plugins as DLLs, I do that in the DllMain of my plugin. The code looks like this (it is inspired by the <a href="http://bobobobo.wordpress.com/2009/03/01/how-to-attach-a-console-to-your-gui-app-in-c/" rel="nofollow">How can I attach a console to my win app?</a> blogpost):</p>
  
  <pre><code>int __stdcall DllMain(void *,unsigned, void *){
        AllocConsole();
        AttachConsole(GetCurrentProcessId());
        FILE* retStream;
        freopen_s(&amp;retStream,"CONOUT$","w",stdout);
        if(!retStream) throw std::runtime_error("Stdout redirection failed.");
        freopen_s(&amp;retStream,"CONOUT$","w",stderr);
        if(!retStream) throw std::runtime_error("Stderr redirection failed.");
        return 1;
    }
  </code></pre>
  
  <p>The window appears when the main program is instructed to load the DLL but disappears within a split second; the DLL is not unloaded as other functions from it get called. What can be the issue? How to keep the console open?</p>
  
  <p>The main program GUI is written using Qt, but again, Qt headers are not part of the program's interface.</p>