Dialog and Console


I am working in a MFC Dialog.

Now this application also needs to be invoked as a Command Line application. I know that I can make use of CWinApp::CCmdLineInfo method. But I have not used earlier.Can I make use of this or should I use anything else ?

If anybodu had used tips would be appreciated.Also suggest me how to implement it.

ALso for a dialog based application error message will get displayed in the form of a Message Box. But for this command line application, I want to display the same error message in the console instead of dialogs/message boxes.Pls let me know how to do the same.

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.

Please clarify.

1)  Are you creating a GUI application that is started from the command-line with parameters?

2)  Are you making a console app?

3)  Are you making a GUI app that just takes flags?
gurukgAuthor Commented:

Basically mine is a dialog based application. I have developed it as a Dialog based application usng MFC - APPWIZARD.

Now I want to enable this application as a console based application also.

Hope this clears your doubt.

Is your plan:
  if command-line arguments are present, treat it as console
  if none are present, treat it as GUI

If so, is it to be presented as GUI when launched from the command-line without arguments?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gurukgAuthor Commented:
I do not know whether I have given my explanation correctly

I accept that if there are command line traet it as Console
But How will I know whether this is a command line based application or not.

For example, by default a MFC dialog based application will not have CommandLineInfo member.

I need to explicitly declare this in InitInstance method of App Class right.

Assume that I had declared this.

Now, my question is how to parse the parameters or how to check for arguments ?

Also, if it is console based application, then I DO NOT WASNT THE DIALOG BOX OR ANY MESSAGE BOXES TO BE DISPLAYED.


gurukgAuthor Commented:
Hi Trisk,

I have done some little R&D which I thought I would like to share.

There is a member called m_lpCmdLine which will give you the command line arguments. I feel this would be helpful.

Ok. I used this.

Assume that from the command prompt I give the following:

I expect the arguments in this form. If the inputs/arguements are not in this form then I would like to print a message in the console itself stating that "Invalid arguments..Usage is..bla..bla..."

How will I achieve this from my DIALOG BASED APPLICATION.

Pls explain me.

Or if this is not possible should I recreate my project as a Win32Console based application ?

Thanks in advance
With the m_lpCmdLine, you'll have to parse the arguments yourself.  You'll get everything entered on the command-line as one string (m_lpCmdLine) and you should call something like strtok() or just scan the string looking for spaces to determine where the next argument resides.

gurukgAuthor Commented:
Hi Trisk,

I have done that.
Everything is coming.

But when invalid number of arguements are typed I want to tell the user that you have entered invalid number of arguements in the console itself directly.

How will I do this ?

Hang on...
I'm investigating "WriteConsole()"
gurukgAuthor Commented:
Hi Hines,
Thanks a lot for your effort

Get it for me.

For the parameters, you may use __argc and __argv, they are global variables that contain the command line arguments:
int __argc - number of arguments
char __argv[] - list of arguments

Wenderson Teixeira
gurukgAuthor Commented:
Hi wenderson,

I got the command line arguements.
Now I want to popup the error in the console itself If I invoke the application from command line

The question is
   How do you display back to the original console calling the program.
I've looked at a lot of options and have not figured it out yet.
If you're in a hurry, you may want to write it as one type only (console OR dialog)
gurukgAuthor Commented:
Hi Trisk,

Yes. That's want I want to do.
if I am allowed to display error message in a separate window then I can call AllocCOnsole right.

But Trisk, I have an idea. Pls validate this:

1)Type your exe name other arguements from the console

2)Now in the code I can get the handle of this CONSOLE window tight just by using API's like FindWindow() .

3 With this handle, can I call WriteConsole for whom I will pass this current console/command window's handle to output the error messages.


I tried that.  No display was given.
gurukgAuthor Commented:
Hi Trisk,

Will this be article of any use ?Pls validate:
Search MSDN for
"Calling CRT Output Routines from a GUI Application"

I could not understand this.

Maybe you could do the other way, create a console application, or just add the define _CONSOLE and change the /subsystem:windows to /subsystem:console in the compiling options, than, if there isn't any command line parameters, you can create a dialog, otherwise, display the messages using a simple printf.
This should work.
By the way, I understood that you already have the command line parameters, I was just pointing out that __argc and __argv are easier to use than lpCmdLine, because the parameters are already correctly parsed, including long file names that contains spaces, ex.:
test "C:\My Folder\My File.TXT"

gurukgAuthor Commented:

But if I create a console based application, if I want to display a dialog then I need to go for SDK Programming right.

I have been asked to develop the application as a Dialog based MFC application.

That's my problem.

I tried the MSDN suggestion and it obviously grabbed the wrong window handle.  I saw a flash of a message on my screen (not in the console window).

I'll keep trying.
I last tried this inside the InitInstance()

     if (m_lpCmdLine[0] != '\0')
          auto     FILE *hf;
          auto     int hCrt = _open_osfhandle((long)
               GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);

          hf = _fdopen( hCrt, "w" );
          *stdout = *hf;
          int i = setvbuf( stdout, NULL, _IONBF, 0 );
          fprintf(hf, "asdf %s asdf", m_lpCmdLine);
          return FALSE;

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
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them updated as the collaboration effort continues, to maintain your open and locked questions.  If you are a  KnowledgePro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer which does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item for help.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  EXPERTS:  Please leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html

Moderators will finalize this question if still open in @7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
Thank you everyone.
Moderator @ Experts Exchange
Zero response, finalized.
Moondancer - EE Moderator
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.