Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dialog and Console

Posted on 2002-03-05
20
Medium Priority
?
443 Views
Last Modified: 2013-11-20
Hi,

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.


Thanks
0
Comment
Question by:gurukg
  • 9
  • 7
  • 2
  • +1
20 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 6841631
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?
0
 

Author Comment

by:gurukg
ID: 6843264
Hi,

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.

Thanks
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6844461
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:gurukg
ID: 6846143
Hi,
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.


Thanks

0
 

Author Comment

by:gurukg
ID: 6847018
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:
c:> <EXE NAME> <USER NAME> <PASSWORD>

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
guru
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6847107
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.

0
 

Author Comment

by:gurukg
ID: 6847115
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 ?

Thanks
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6847132
Hang on...
I'm investigating "WriteConsole()"
0
 

Author Comment

by:gurukg
ID: 6847140
Hi Hines,
Thanks a lot for your effort

Get it for me.

Thanks
0
 

Expert Comment

by:wenderson
ID: 6849889
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
0
 

Author Comment

by:gurukg
ID: 6849894
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

0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6850191
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)
0
 

Author Comment

by:gurukg
ID: 6850209
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.

Thanks,
guru

0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6850220
I tried that.  No display was given.
0
 

Author Comment

by:gurukg
ID: 6850235
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.

Thanks
0
 

Expert Comment

by:wenderson
ID: 6852305
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"

Wenderson
0
 

Author Comment

by:gurukg
ID: 6852312
hI wENDERSON,
tHANKS FOR YOUR SUGGESTION.

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.

Thanks
0
 
LVL 6

Accepted Solution

by:
Triskelion earned 200 total points
ID: 6852651
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;
          AllocConsole();
         
          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;
          }
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6938100
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
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.
http://www.experts-exchange.com/questions/Q.20251939.html
http://www.experts-exchange.com/questions/Q.20252091.html
http://www.experts-exchange.com/questions/Q.20251940.html
http://www.experts-exchange.com/questions/Q.20258960.html
http://www.experts-exchange.com/questions/Q.20270150.html
http://www.experts-exchange.com/questions/Q.20270163.html
http://www.experts-exchange.com/questions/Q.20273461.html
http://www.experts-exchange.com/questions/Q.20279250.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20273913.html

**** PLEASE DO NOT AWARD THE POINTS TO ME. *****
 
------------>  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.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7116526
Zero response, finalized.
Moondancer - EE Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question