1

I have a Windows application application that is normally GUI-only and declares the WINDOWS subsystem. It doesn't open a console window when launched.

Alas, I would like to offer additional console output when the application happens to be started from console window (say, from interactive cmd.exe).

Is there a way of detecting if some process "up the chain" has an open console, and to attach to that console?

The closest I've found is a way to explicitly open a console for a gui application, but I don't want to open a console if there isn't one already there.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • 1
    AttachConsole(). It invariably works poorly, it is already in use by the command processor. You'll fight over input, the command processor goes first. Only consider AllocConsole(). – Hans Passant Oct 03 '13 at 21:49

1 Answers1

3

At first glance it seems like the AttachConsole() function will let you attach to the console of your parent process:

AttachConsole(ATTACH_PARENT_PROCESS);

If the process doesn't actually have a console the function will fail with ERROR_INVALID_HANDLE. The function will also fail if your parent process no longer exists. If it fails you can then call AllocConsole() to create your own.

I've never tried this so don't actually know if it will work or not :)

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • 1
    It works! Yay! *Exactly* what I looked for. Now I'll figure out how to get output on said console. – Kuba hasn't forgotten Monica Oct 03 '13 at 20:52
  • This doesn't work. Well, you can certainly attach and get output onto the console. All you need to do is `printf`, `cout` or whatever. The problem is that you now have two processes attached to the console. Both of them can write which makes a mess. And the parent process will not wait for the child, so if the parent quits, then what happens to the console? This question has been asked so many times. It never ends well. – David Heffernan Oct 03 '13 at 20:56
  • @DavidHeffernan: "A console is closed when the last process attached to it terminates or calls FreeConsole" according to the docs. – Jonathan Potter Oct 03 '13 at 21:05
  • That's also weird though. Imagine closing your cmd terminal with exit and it not going away. You really don't want two active processes sharing a console. – David Heffernan Oct 03 '13 at 21:13
  • Presumably the OP does since that's the question he asked. – Jonathan Potter Oct 03 '13 at 21:15
  • Yes, I'm quite sure that the asker has considered all these issues. – David Heffernan Oct 03 '13 at 21:21
  • 1
    It's really simple. When the program is started from the console with wrong parameters, I want to report errors to the console and immediately terminate. When it's started without a console, the process respawns itself with an argument that merely shows a dialog box with a message about what went wrong. In either case the original process terminates with an error code. It works just fine. I even managed to reorganize console output so that it looks like my output was inserted between the cmd.exe prompts. – Kuba hasn't forgotten Monica Oct 04 '13 at 00:54