Solved

Dialog and Console

Posted on 2002-03-05
20
425 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:gurukg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hang on...
I'm investigating "WriteConsole()"
0
 

Author Comment

by:gurukg
Comment Utility
Hi Hines,
Thanks a lot for your effort

Get it for me.

Thanks
0
 

Expert Comment

by:wenderson
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:gurukg
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I tried that.  No display was given.
0
 

Author Comment

by:gurukg
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Zero response, finalized.
Moondancer - EE Moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now