Re: Using AttachConsole() to run GUI app in command prompt console

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Gary Chanson (gchanson_at_No.Spam.TheWorld.net)
Date: 04/20/04


Date: Tue, 20 Apr 2004 01:46:40 -0400


"Pieter" <msnews@insanegenius.com> wrote in message
news:ul0uThoJEHA.2604@tk2msftngp13.phx.gbl...
> How to run a GUI app in the command prompt where it was launched, as if
GUI
> app is a CONSOLE app.
>
> I previously posted a thread and got some good input, now I am continuing
my
> investigation of using AttachConsole() for a XP only solution.
> See previous post: "Running a GUI app in CONSOLE mode, alternatives to
> AttachConsole() and .com file"
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/attachconsole.asp
> http://msdn.microsoft.com/msdnmag/issues/04/02/CQA/default.aspx
>
> I have a windows GUI subsystem app that normally runs as a windowed app.
> The app can be driven using commandline parms.
> If the app is invoked from a console, i.e. a command prompt, the app
should
> output to the command prompt window. (like a console app would do)
> If the app is not invoked from a console, i.e. explorer, the app should
> output nothing. (like a windows app would do if not displaying a window)
>
> I created a test GUI app that in winmain calls
> AttachConsole(ATTACH_PARENT_PROCESS).
> I launch the app from a command prompt.
> Any text output is displayed in the command prompt.
> But, I can continue typing in the command prompt, i.e. the AttachConsole()
> did not take ownership of the console.
>
> I used GetStdHandle(STD_INPUT_HANDLE) to get a handle to the input
console.
> I am using ReadConsoleInput() to capture keyboard events, but I am not
> getting any key events from my attached console.
> I also tried calling SetConsoleMode(), but I still do not receive any key
> events.
>
> - How can I completely take over the parent console window?
> - How can I get console input events?
> - I assume that a command prompt that runs a console application waits for
> the console app to terminate, but a command prompt that runs a windows app
> does not wait. Is this correct?

    Yes and that means that you are out of luck. No program "takes over the
parent console". It is up to the calling program to launch your program and
then either to wait or to continue immediately. If it chooses to continue,
you simply have two concurrently running processes using the same output
channel. You can't reach inside the console app which launched your program
to tell it to wait for your program to terminate.

    As far as console input events, nothing is stopping your program from
receiving them except that the parent program may get there first (it's
probably just sitting there waiting for them).

    Unfortunately, the Windows console subsystem is too broken to fix.

-- 
-GJC [MS Windows SDK MVP]
-Software Consultant (Embedded systems and Real Time Controls)
- http://www.mvps.org/ArcaneIncantations/consulting.htm
-gchanson@mvps.org


Relevant Pages