Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several approaches for applications that need to choose whether to act as console or GUI applications, depending on context, on Windows:</p> <ol> <li>Have two separate applications, and have one conditionally start the other.</li> <li>A variant of the above strategy, have two applications, one called 'app.com' (i.e. just rename a console EXE with COM extension) and the other called 'app.exe', so that command-line invocations will find app.com first. Because of ancient DOS compatibility, .COM executables are found before .EXEs. (This in configurable in Windows; see the PATHEXT environment variable.)</li> <li>The rxvt/Cygwin technique, which is one I haven't really seen documented anywhere else.</li> </ol> <p>Let me go into a little bit of detail about how rxvt on Cygwin works. Rxvt is a terminal emulator that normally runs on the X Window system. Because of the limitations of the Win32 console, Cygwin packages it as a more fully-featured console, with support for things like lots of lines of history, dynamic resizing, per-instance configurable fonts and colour themes, non-application-freezing mouse select and copy, etc. In order to run natively on Windows, rxvt shipped with Cygwin includes a tiny X11 wrapper library for Win32. Rxvt on Windows is actually a console application for compatibility reasons with existing native Win32 executables, but most of the time you never see the console; you just see the rxvt terminal emulator window itself.</p> <p>The way it works is specifically implemented in <code>rxvt/W11/wrap/wrap.c</code> in the rxvt source tree, in the function called <code>hideConsole()</code>. Basically, it opens up its console (with a <code>CreateFile("CONOUT$" ...)</code>), and checks to see if the cursor position is at (0,0) (using <code>GetConsoleScreenBufferInfo()</code> on the console handle).</p> <p>If it is, then it infers that it has been started as a standalone application, rather than from a console parent application, and thus it knows the OS has created a dedicated Win32 console for the process. It proceeds to hide this console window, but it has to find it first. It uses <code>SetConsoleTitle</code> to set the console window's caption to a unique value based on the name of the application and the current thread ID. It then uses <code>FindWindow</code> to find this window's handle (periodically <code>Sleep</code>ing for a few ms if necessary for the title to change, because the console windows are actually controlled by a different process entirely in Windows). When it eventually finds the window handle, it hides it with <code>ShowWindowAsync</code>, passing in <code>SW_HIDE</code>.</p> <p>Using this approach, you can write an application that:</p> <ul> <li>if started from a console parent, it can continue to use this console</li> <li>if started as an application, it can optionally choose whether or not to hide the console</li> </ul> <p>The only downside is a very brief flash of a console window at application startup.</p>
 

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