WOWTO: console stdout/printf and GUI-based MFC application 
Author Message
 WOWTO: console stdout/printf and GUI-based MFC application

Hi,

   I have created a GUI-based MFC application ( VC6 ) and I want to add a
command line option that
will output something on the console ( only when the application is launched
from a console ) without having any
GUI object displayed.

Example :

C:\>myMdiApp.exe -V
myMdiApp version 1.0.0.1

C:\>

    ---- or ----

C:\>myMdiApp.exe -V > info.txt
C:\>

    ---- or ----

C:\>myMdiApp.exe                         // this will launch the application
( with MDI windows, etc... )

Thanks,

        Franck



Tue, 08 Feb 2005 16:04:45 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application

Checkout the ProcessShellCommand function in your
application source InitInstance. The command line
arguments in an application can be controlled there.

Quote:
>-----Original Message-----
>Hi,

>   I have created a GUI-based MFC application ( VC6 ) and
I want to add a
>command line option that
>will output something on the console ( only when the

application is launched
Quote:
>from a console ) without having any
>GUI object displayed.

>Example :

>C:\>myMdiApp.exe -V
>myMdiApp version 1.0.0.1

>C:\>

>    ---- or ----

>C:\>myMdiApp.exe -V > info.txt
>C:\>

>    ---- or ----

>C:\>myMdiApp.exe                         // this will

launch the application

- Show quoted text -

Quote:
>( with MDI windows, etc... )

>Thanks,

>        Franck

>.



Tue, 08 Feb 2005 16:19:22 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application
In fact, my aim is to output some text ( if the option is -V ) in the console
from where my application is launched.

Case 1: my application is launched without any command-line option
 => the application is launched in GUI mode ( with MDI windows, etc... ).

Case 2: my application is launched with a -V as command-line option (
myMdiApp.exe -V )
  => the application displays "myMdiApp version 1.0.0.1" and exits immediately.


| Hi,
|
|    I have created a GUI-based MFC application ( VC6 ) and I want to add a
| command line option that
| will output something on the console ( only when the application is launched
| from a console ) without having any
| GUI object displayed.
|
| Example :
|
| C:\>myMdiApp.exe -V
| myMdiApp version 1.0.0.1
|
| C:\>
|
|     ---- or ----
|
| C:\>myMdiApp.exe -V > info.txt
| C:\>
|
|     ---- or ----
|
| C:\>myMdiApp.exe                         // this will launch the application
| ( with MDI windows, etc... )
|
|
|
| Thanks,
|
|
|         Franck
|
|



Tue, 08 Feb 2005 16:43:27 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application

Checkout the ProcessShellCommand function in your
application source InitInstance. The command line
arguments in an application can be controlled there.

Quote:
>-----Original Message-----
>In fact, my aim is to output some text ( if the option

is -V ) in the console
Quote:
>from where my application is launched.

>Case 1: my application is launched without any command-
line option
> => the application is launched in GUI mode ( with MDI
windows, etc... ).

>Case 2: my application is launched with a -V as command-
line option (
>myMdiApp.exe -V )
>  => the application displays "myMdiApp version 1.0.0.1"

and exits immediately.
Quote:



>| Hi,
>|
>|    I have created a GUI-based MFC application ( VC6 )
and I want to add a
>| command line option that
>| will output something on the console ( only when the

application is launched
Quote:
>| from a console ) without having any
>| GUI object displayed.
>|
>| Example :
>|
>| C:\>myMdiApp.exe -V
>| myMdiApp version 1.0.0.1
>|
>| C:\>
>|
>|     ---- or ----
>|
>| C:\>myMdiApp.exe -V > info.txt
>| C:\>
>|
>|     ---- or ----
>|
>| C:\>myMdiApp.exe                         // this will

launch the application

- Show quoted text -

Quote:
>| ( with MDI windows, etc... )
>|
>|
>|
>| Thanks,
>|
>|
>|         Franck
>|
>|

>.



Tue, 08 Feb 2005 16:52:35 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application


Quote:
> In fact, my aim is to output some text ( if the option is -V ) in the
console
> from where my application is launched.

> Case 1: my application is launched without any command-line option
>  => the application is launched in GUI mode ( with MDI windows, etc... ).

> Case 2: my application is launched with a -V as command-line option (
> myMdiApp.exe -V )
>   => the application displays "myMdiApp version 1.0.0.1" and exits
immediately.

I don't know if you can have an app that is both a console app and a GUI
app. (I actually wanted to do this myself once, and I gave up.)

One solution: have a console app that checks the command line and prints out
as above in the "-V" case. If "-V" is not present launch the separate GUI
app. In other words, make a stub console app as a front end for your GUI
app.

HTH

Jay



Wed, 09 Feb 2005 01:31:54 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application

Quote:

>Hi,

>   I have created a GUI-based MFC application ( VC6 ) and I want to add a
>command line option that
>will output something on the console ( only when the application is launched
>from a console ) without having any
>GUI object displayed.

You can't do this *as you described*.  Instead, rely on bizarre
behaviour in the Windows command processors.

Compile a small console app, and call it myMdiApp.com (not .exe).  Its
job is to process the -V option and use stdout.

Compile the MDI GUI app, and call it myMdiApp.exe.  Its job is to have a
GUI.

Keep the two apps in the same directory.

If you don't give myMdiApp.com the -V option, it should retrieve
argv[0], chop the .com off, replace it with .exe, and then append all
the other arguments and pass the result to CreateProcess.

Why does this work?  When CMD.EXE and COMMAND.COM (the command
processors) process a command line that is not a built-in command, they
search the current directory and then the directories in the PATH
variable for a file with the requested name and one of these extensions:

COM
EXE
BAT
CMD (NT/2K/XP only)

CMD.EXE will then scan the directories for files with extensions in the
PATHEXT variable as well.  The scan looks for each extension before
going to the next directory.

When you type "myMdiApp {params}", the command processor finds
myMdiApp.com first, and executes it.  When myMdiApp.com replaces .com
with .exe, CreateProcess loads myMdiApp.exe from the same directory.

It's a bit of a kludge, but it's what Visual C++ does when you type
MSDEV into a command window.

--
"Eagle-eyed" Steve

The pictures they didn't want me to take:
        http://www.koolpages.com/theoddcar/
or:
        http://www.grandfathersaxe.demon.co.uk/



Wed, 09 Feb 2005 03:41:06 GMT  
 WOWTO: console stdout/printf and GUI-based MFC application


<snip>

Quote:
> I don't know if you can have an app that is both a console app and a GUI
> app. (I actually wanted to do this myself once, and I gave up.)

You can, but you have more work to do. In order to properly
reuse the console window it's launched from, you need to
make a _console_ app.

Quote:
> One solution: have a console app that checks the command line and prints
out
> as above in the "-V" case. If "-V" is not present launch the separate GUI
> app. In other words, make a stub console app as a front end for your GUI
> app.

That's the easiest way to do it if you already have a GUI
app. The stub itself actually only has to launch the GUI,
in the appropriate way.

The problem is that you need to control how CreateProcess
is called on the GUI app to override default behavior and
give it access to the original console. By default, GUI apps
are launched _detached_, and console apps are launched
_attached_. By the time the app is launched, the decision
has been made.

From MSDN: Platform SDK: DLLs, Processes, and Threads

"Creation of a Console...
A GUI or console process can use the CreateProcess function
to create a new console process and specify a flag to tell the
system to create a new console...
GUI processes are not attached to any console when they
are created."

This means it has _no access_ to the console of the command
processor.

"A console process is not attached to a console if the
DETACHED_PROCESS flag was specified in a call to
CreateProcess when the process was created."

Which is _not_ specified by the command processor, so
the console app is attached to the parent's console.

It's just a matter of default behavior. Console apps can
create windows, and GUI apps can allocate consoles.



Wed, 09 Feb 2005 04:35:41 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. WOWTO: console stdout/printf and GUI-based MFC application

2. stdout from MFC based GUI to dos box

3. stdout from mfc based GUI app into a dos box

4. HOWTO printf to stdout/stderr from MFC application ?

5. stdout from Console Child to GUI Parent

6. stdout from console child to GUI parent

7. Help needed to convert Console Based appln to a MFC Application

8. Q: MFC & stdout/stderr output dual application (was HOWTO printf to stdout/stderr in MFC)

9. Console based program to MFC based project

10. Uses printf() in a GUI application.

11. Redirecting stdout from child console application

12. Pipes from GUI to console application

 

 
Powered by phpBB® Forum Software