Link to home
Start Free TrialLog in
Avatar of gurukg
gurukg

asked on

Dialog and Console

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
Avatar of Triskelion
Triskelion
Flag of United States of America image

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?
Avatar of gurukg
gurukg

ASKER

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
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?
Avatar of gurukg

ASKER

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

Avatar of gurukg

ASKER

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
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.

Avatar of gurukg

ASKER

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
Hang on...
I'm investigating "WriteConsole()"
Avatar of gurukg

ASKER

Hi Hines,
Thanks a lot for your effort

Get it for me.

Thanks
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
Avatar of gurukg

ASKER

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)
Avatar of gurukg

ASKER

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

I tried that.  No display was given.
Avatar of gurukg

ASKER

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
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
Avatar of gurukg

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Triskelion
Triskelion
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://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.
https://www.experts-exchange.com/questions/Q.20251939.html
https://www.experts-exchange.com/questions/Q.20252091.html
https://www.experts-exchange.com/questions/Q.20251940.html
https://www.experts-exchange.com/questions/Q.20258960.html
https://www.experts-exchange.com/questions/Q.20270150.html
https://www.experts-exchange.com/questions/Q.20270163.html
https://www.experts-exchange.com/questions/Q.20273461.html
https://www.experts-exchange.com/questions/Q.20279250.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://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 https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> https://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
Zero response, finalized.
Moondancer - EE Moderator