Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Line Arguments with MFC

Status
Not open for further replies.

nytyme

Programmer
Jan 8, 2001
19
US
I'm not used to dealing with MFC, and my knowledge of C++ is still very amature.

i've developed an application that needs to run minimized or hidden, so i used the
MFC application. i want to continue using it, so please no suggestions to the effect
of using a different method.

what i need to know, is how do i use Command Line Arguments? I looked on msdn.microsoft.com
and wasn't able to follow it. if you could please show a quick easy example, it would be much appreciated. what i was able to grasp is that it must be done in the InitInstance() function (i used the MFC wizard).

thanks,
Nytym
 
Commandline arguments are defined when the main function is called. For example if I wanted a program that would check if the command line argument was greater than a particular number firstly I would define the main function as below:

int main(int argc, int *argv)

num will calculate how many arguments are inputed into the program so you might have a statement in your program saying. argv is a pointer variable where the command line inputs will be stored:

if (num<1)
{
cout << &quot;You must give at least 1 command line option&quot;
)

(you'll also need one for >1 (so you should just use == 1 ub your if statement) as you have only declared a single variable in main, if you want to use more than one command line option then declare argv as an array (ie int *argv[]))

next you'll want to actualyl check if argv is above a certain number so you can just add the statement:

if (argv>3)
{
statement
}

which will check that the option is greater than 3.

if you wish to use character options on the command line instead of integer then quite simply instead of declaring int *argv, in main just declare char *argv . Also if you want to use string options, just declare an array of characters in main, ie char *argv[] .

Hope this helps
 
If you really are using MFC just do this in InitInstance:

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;

I took this straight from the MFC AppWizard code for a single document view application.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top