MFC GUI app started from DOS window

It seems to me that this has been addressed before but (as usual) I can't find it when I need it.....

I have an MFC GUI application.  I am adding a command line capability.  If the app is started from a command line it should:

1) Detect that it's been started this way, if possible.  If it's not possible, then I'm OK with just using a command line arg to specify non-GUI.

2) If started from the command line, be able to input/output to the existing console with printf, etc.  I know about ::AllocConsole() but this creates a new console.  I want to use the existing one.
LVL 32
jhanceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
#1 can be done, but only on NT by detecting the process parent...

#2 can only be achieved by using a 'proxy' non-GUI app (would make #1 obsolete...)
0
jhanceAuthor Commented:
#1 is not that important, certainly not important enough to do a NT specific thing....

#2 Are you sure?  I've seen this done elsewhere with just a single app and I thought I'd seen a write up on this somewhere...
0
brandtsCommented:
I've never tried to do anything like what you're asking, but I think what you need to do is create separate main and winmain functions.  Console applications use the function main as their entry point, while Win32 applications use winmain.  I'm afraid I can't give you more details than that, though.

The VC++ help has information in article mk:@ivt:vclang/F8/DA/S1D8B0.HTM.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

jhanceAuthor Commented:
brandts:

mk:@ivt:vclang/F8/DA/S1D8B0.HTM

is not very helpful.  These links are MSDN version specific.  What's the title of the article?
0
jkrCommented:
I'm *quite* sure about #2, as I desparately tried to do the same a while ago (well, I'm not perfect ;-) - the problem is that a console app can display GUI elements to the user, but a GUI app cannot connect to the console it was started from. The last resort is 'AllocConsole()' :-(
0
mikeblasCommented:

 jhance> It seems to me that this has been addressed before but (as usual)
 jhance> I can't find it when I need it.....

I address this issue at great length in my book.

 jhance> I have an MFC GUI application.  I am adding a command line capability.
 jhance> If the app is started from a command line it should:

 jhance> 2) If started from the command line, be able to input/output to the existing
 jhance> console with printf, etc.  I know about ::AllocConsole() but this creates
 jhance> a new console.  I want to use the existing one.

You can't do this.

If you link your applicaiton /SUBSYSTEM:WINDOWS, you'll not get access to the existing console if started from a console window. You can, of course, create windows and do whatever you want.

If you link your application /SUBSYSTEM:CONSOLE, you'll always get a console window--that you can't close!--even if you're launched from some part of the GUI.

..B ekiM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhanceAuthor Commented:
Mike,

Thanks for the comment.  

Yes, I did read about this in your book and reviewed the HYBRID sample.  It doesn't deal with this specifically.  HYBRID creates a new console from a GUI, it doesn't link a existing app to an existing console.

I was almost positive I'd seen something else on this topic, perhaps a Dr. GUI note or something but I could be mixed up....

Oh well, I guess it's on to plan "B".


Regards...
0
jkrCommented:
>>If you link your application
>>/SUBSYSTEM:CONSOLE, you'll always get
>>a console window--that you can't
>>close!--even if you're launched from
>>some part of the GUI.

'FreeConsole()'
0
mikeblasCommented:
> 'FreeConsole()'

FreeConsole() can close the console you get if your subsystem console app is launched from a subsystem Windows app, but:

1) You'll find that all the standard handles you used to have are broken (eg, stdout, stderr, cout, etc.)

2) You'll still suffer a flash as the console window appears and disappears.

I'd have to check with the systems guys, but I'd really recommend against using it--having your app free something it didn't allocate seems dubious to me.

..B ekiM

0
jkrCommented:
You're right, I just wanted to object to "that you can't close!", which - as your comment implies - can be done...

>>but I'd really recommend against using it

It comes in handy to detach an application from the command line to do background processing (and should not occupy the console - it can be compared to 'daemonizing' a process on UN*X) - IMHO it's just a matter of what you want to do...
0
jkrCommented:
You're right, I just wanted to object to "that you can't close!", which - as your comment implies - can be done...

>>but I'd really recommend against using it

It comes in handy to detach an application from the command line to do background processing (and should not occupy the console - it can be compared to 'daemonizing' a process on UN*X) - IMHO it's just a matter of what you want to do...
0
mikeblasCommented:

 "can be done..." isn't always the same as "a correct and safe thing to do".

..B ekiM
0
jkrCommented:
Well, unfortunately not always. But it is in this special case, otherwise I wouldn't have mentioned it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.