Discussion:
Using AttachConsole() to run GUI app in command prompt console
(too old to reply)
Pieter
2004-04-20 03:34:05 UTC
Permalink
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?


Thanks

Pieter
Gary Chanson
2004-04-20 05:46:40 UTC
Permalink
Post by Pieter
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
Post by Pieter
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
-***@mvps.org
Pieter
2004-04-21 00:23:45 UTC
Permalink
Thanks, it was worth a try.
Post by Pieter
Post by Pieter
How to run a GUI app in the command prompt where it was launched, as if
GUI
Post by Pieter
app is a CONSOLE app.
I previously posted a thread and got some good input, now I am
continuing
Post by Pieter
my
Post by Pieter
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
Post by Pieter
Post by Pieter
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
Post by Pieter
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()
Post by Pieter
Post by Pieter
did not take ownership of the console.
I used GetStdHandle(STD_INPUT_HANDLE) to get a handle to the input
console.
Post by Pieter
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
Rhett Gong [MSFT]
2004-04-21 07:12:12 UTC
Permalink
After we discussed in your previous post, my thought truns to the entry point of an .exe. Since when we link an application, we use /SUBSYSTEM:CONSOLE to
indicate mainCRTStartup and use /SUBSYSTEM:WINDOWS to indicate WinMainCRTStartup. And we can use the linker option /ENTRY to specify the starting
address for an .exe, I thought if we could use "/SUBSYSTEM:WINDOWS /ENTRY:myMain" I do something like this (pseudo code):
//-----------------------------------------------------------------------------------
int __stdcall main(int argc, char **argv)
{
If( argv == GUI)
{
// call
hInst = GetModuleHandle(NULL);
myWMain(hInst,NULL,NULL,1);
//...........
}
else
{
// execute useful functions here
}
return 0;
}
//----------------------------------------------------------------------------------

Good Luck!
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.

Continue reading on narkive:
Loading...