xterm is a console program?

Benjamin Riefenstahl Benjamin.Riefenstahl@epost.de
Sat May 17 20:38:00 GMT 2003


Hi Early,


> "Karl Waclawek" <karl@waclawek.net> wrote:
>> > Case one
>> > 4) Double-clicked X apps should not open a command-prompt window
>> > at all.

"Early Ehlinger" <early@respower.com> writes:
> This is rather easy to implement, as I mentioned in my
> thread-starting post - simply have a WinMain that parses
> GetCommandLine() and then calls main().

This is already in the runtime, as I said before.

Just to clarify: The distinction between WinMain() and main() is not a
requirement of the OS, but only a programming convention.  Technically
both console apps and GUI apps are free to use either.  If you use
main(), the C runtime will provide the glue to do what is necessary.

What you actually need is the -mwindows switch to the linker, so that
the linker sets the flag in the PE header of the binary that tells
Windows not to treat the app as a console app, but as a GUI app
instead.

>> > Case two
>> > 3) *CRUCIAL PART* The X app, being very smart, notices that it
>> > was launched from a console window and attaches its text output
>> > to that very same console window.  NO NEW CONSOLE IS CREATED.
>> >
>> > 4) Any text error messages (via printf, etc.) from the X app go
>> > to the command prompt window that the X app was launched from.
>
> Can't an X app check GetConsoleWindow()?

Sure, but GetConsoleWindow() will always return NULL for GUI apps
AFAIK.  That's the problem.

I have spent some time researching this on NT4 a couple of years ago.
As far as I understand the issue, it's like this:

The Win32 API doesn't have the concept of a parent process, instead it
has the concept of a process group.  All applications that run in a
console are part of the same process group.  Calling
GetConsoleWindow(), Opening and using $CONOUT and $CONIN, all this
refers to the console of the process group.

If a process that has access to a console calls CreateProcess() to
start a console application, the new process will inherit the process
group, and so the link to the console.

Problem is, whenever a GUI process is started, the new process will
never inherit the process group, instead it always creates a new
process group.  A GUI process doesn't have access to a console, unless
it creates it's own with AllocConsole().


Hope this helps, benny



More information about the Cygwin-xfree mailing list