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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Comand Line Arguments

Status
Not open for further replies.

reneb

Programmer
Aug 8, 2000
43
US
I have a Dialog based MFC Application. I want to retrieve the command line arguments on the InitInstance() function of my App Class. How do I do this if the function has no parameters? Thanks in advance.
 
The command line is stored as a member of your CWinApp-derived class as m_lpCmdLine. You can parse this variable however you want.
 
Hi

The brave guys at Microsoft have designed something to parse command line the easy way ( hmmmm...)

First set up a class as followed:

class CDemoCmdLineInfo : public CCommandLineInfo
{
// Constrcutor
public:
CDemoCmdLineInfo();

// Overrides
// Parse Parameters of Command Line
virtual void ParseParam( const char* pszParam, BOOL bFlag, BOOL Last);
};

CDemoCmdLineInfo::CDemoCmdLineInfo()
{

}

void CDemoCmdLineInfo::parseParam( const char* pszParam,
BOOL bFlag, BOOL Last)
{

CString strParam = pszParam;
CString str;

str.Format( "Parameter ::%s:: Last One ? %s \n", pszParam, Last ? "TRUE" :"FALSE");
::OutputDebugString( str);

CCommandLineInfo::parseParam( pszParam, bFlag, Last);
}

Add the following line in OnInitInstance:

// Parse Parameters of Command Line

CDemoCmdLineInfo CmdLine;

ParseCommandLine( CmdLine);

Just before the line
int nResponse = dlg.DoModal();

To test it, modify the settings 'Program Arguments' in the 'Debug Tab' to something like this;

-Mode:Demo -Line:1234 -File:c:\demo

If you run the program, you should have the following lines in the 'Debug' tag of the Output window

Parameter ::Mode:Demo:: Last One ? FALSE
Parameter ::Line:1234:: Last One ? FALSE
Parameter ::File:c:\demo:: Last One ? TRUE

Of course, now you'll have to dig a few in the doc. and handle the possible erroneous parameters entered by the user. At least, you already got them.
Note that -Mode:, -Line:, .. are not required.

HTH

Thierry
EMail: Thierry.Marneffe@swing.be

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top