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!

is there argv, argc in Visual C++?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do we read in 2 string type arguments from command line?

I am trying to use "GetCommandLine()" ..... but it seems to be reading in the working dir by default first and then my 2 arguments. Also how do i separate between the 2 arguments.

Thank you
 
Hi

I suppose you are using MFC ...

First, derive a class from CCommandLineInfo

Then override the function ParseParam. Here is an example thazt check for DEBUG parameter and also store the last command line parameter in the variable m_strFile

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

CString strDebug = "DEBUG";

// Check for /Debug Parameter

if ( bFlag)
{
// Check for DEBUG Sub-String

if ( strDebug.CompareNoCase( pszParam) == 0)
m_bDebug = TRUE;
}
else
{
// Get File Name from Command Line

m_strFile = pszParam;
}

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

Call you class instance like this in OnInitInstance function

In this example, the parameter is transfered to the dialog box that is started in OnInitInstance ...

// Parse Parameters of Command Line

CFooCommandLineInfo CmdLine;

ParseCommandLine( CmdLine);

// Transfer Data to Dialog Class

dlg.m_strFile = CmdLine.m_strFile;

// Start the Dialog Box

dlg.DoModal();

Hope it's clear and that it will help

Thierry
 
Thanx for the help. Yes, I am using MFC. Actually I am using the command line argument as the "program name" ShellExecute. In other words, my MFC program gets the name of .exe file from command line and launches it.

There must be a easier and quick way to do this. Since "GetCommandLine()" does read in the cmd line arg. Except it prepends the name of working dir like this.

CString s = GetCommandLine();

and if you view s: "NAME_OF_WORKING_DIR" "ARGUMENT".

So if I parse this, I am ok. But I want to find a quicker way as I will be calling it often. Anyone ????
 
Since a front slash is not vaid in a file name or directory name, use it to mark your parameter.

Such as: c:\myprogdir\myprog.exe /calc

CString S = GetCommandLine();
S = S.Mid((S.Find("/", 0) + 1), (S.GetLength() - S.Find("/", 0)));

//S will equal calc

Hope it helps.
 
Jsaxon ..... that's a thot for future. I like that.

But i just did the old schooling parsing and that works too:

1) look for a " and then the second " .....
2) if no quote is present in first postion, Look for blank cpace.

then copy everything to the end.

It's such a shame that a simple "argv" is not included in VC++ .....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top