Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do writes to stdout go when launched from a cygwin shell, no redirection
    primarykey
    data
    text
    <p>I have an application, let's call it myapp.exe, which is dual-mode console/GUI, built as /SUBSYSTEM:WINDOWS (There's a tiny 3KB shim myapp.com to cause cmd.exe to wait to display the new prompt.)</p> <p>If I launch from a command prompt:</p> <ul> <li><code>myapp</code> -> cmd.exe runs myapp.com which runs myapp.exe. stdout is initially a detached console, by using <code>AttachConsole</code> and <code>freopen("CONOUT$", "w", stdout)</code> my output appears in the command box. OK</li> <li><code>myapp.exe</code> -> cmd.exe displays the prompt too early (known problem), otherwise same as previous. Not a normal usage scenario.</li> <li><code>myapp &gt; log</code> -> stdout is a file, normal use of <code>std::cout</code> ends up in the file. OK</li> </ul> <p>If I launch from Windows explorer:</p> <ul> <li><code>myapp.com</code> -> console is created, stdout is console, output goes into console. Same result as using /SUBSYSTEM:CONSOLE for the entire program, except that I've added a pause when <code>myapp.com</code> is the only process in the console. Not a normal usage scenario.</li> <li><code>myapp.exe</code> -> stdout is a NULL handle, I detect this and hook <code>std::cout</code> to a GUI. OK</li> </ul> <p>If I launch from Matlab shell:</p> <ul> <li><code>system('myapp')</code> or <code>system('myapp.com')</code> or <code>system('myapp.exe')</code> -> For all three variations, stdout is piped to MatLab. OK</li> </ul> <p>If I launch from a cygwin bash shell:</p> <ul> <li><code>./myapp.com</code> -> Just like launch from cmd.exe, the output appears in the command box. OK</li> <li><code>./myapp</code> -> (bash finds <code>./myapp.exe</code>). <strong>This is the broken case</strong>. stdout is a non-NULL handle but output goes nowhere. This is the normal situation for running the program from bash and needs to be fixed!</li> <li><code>./myapp &gt; log</code> -> Just like launch from cmd.exe with file redirection. OK</li> <li><code>./myapp | cat</code> -> Similar to file redirection, except output ends up on the console window. OK</li> </ul> <p>Does anybody know what cygwin sets as stdout when launching a /SUBSYSTEM:WINDOWS process and how I can bind <code>std::cout</code> to it? Or at least tell me how to find out what kind of handle I'm getting back from <code>GetStdHandle(STD_OUTPUT_HANDLE)</code>?</p> <p>My program is written with Visual C++ 2010, without <code>/clr</code>, in case that matters in any way. OS is Windows 7 64-bit.</p> <p>EDIT: Additional information requested.</p> <p>CYGWIN environment variable is empty (or non-existent).</p> <p><code>GetFileType()</code> returns <code>FILE_TYPE_UNKNOWN</code>. <code>GetLastError()</code> returns <code>6 (ERROR_INVALID_HANDLE)</code>. It doesn't matter whether I check before or after calling <code>AttachConsole()</code>.</p> <p>However, if I simply ignore the invalid handle and <code>freopen("CONOUT$", "w", stdout)</code> then everything works great. I was just missing a way to distinguish between (busted) console output and file redirection, and <code>GetFileType()</code> provided that.</p> <p>EDIT: Final code:</p> <pre><code>bool is_console(HANDLE h) { if (!h) return false; ::AttachConsole(ATTACH_PARENT_PROCESS); if (FILE_TYPE_UNKNOWN == ::GetFileType(h) &amp;&amp; ERROR_INVALID_HANDLE == GetLastError()) { /* workaround cygwin brokenness */ h = ::CreateFile(_T("CONOUT$"), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (h) { ::CloseHandle(h); return true; } } CONSOLE_FONT_INFO cfi; return ::GetCurrentConsoleFont(h, FALSE, &amp;cfi) != 0; } bool init( void ) { HANDLE out = ::GetStdHandle(STD_OUTPUT_HANDLE); if (out) { /* stdout exists, might be console, file, or pipe */ if (is_console(out)) { #pragma warning(push) #pragma warning(disable: 4996) freopen("CONOUT$", "w", stdout); #pragma warning(pop) } //std::stringstream msg; //DWORD result = ::GetFileType(out); //DWORD lasterror = ::GetLastError(); //msg &lt;&lt; result &lt;&lt; std::ends; //::MessageBoxA(NULL, msg.str().c_str(), "GetFileType", MB_OK); //if (result == FILE_TYPE_UNKNOWN) { // msg.str(std::string()); // msg &lt;&lt; lasterror &lt;&lt; std::ends; // ::MessageBoxA(NULL, msg.str().c_str(), "GetLastError", MB_OK); //} return true; } else { /* no text-mode stdout, launch GUI (actual code removed) */ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload